Título: como leer mediante substring el codigo de un ubigeo en SQL Publicado por: pokareiser en 7 Marzo 2012, 19:44 pm El asunto es este tengo una tabla llamada ubigeo k consta de los sgtes campos:
cod_dep char(2) cod_prov char(2) cod_dist char(2) nombre varchar(50) ejm: cod_dep cod_prov cod_dist Nombre 15 00 00 LIMA 15 01 17 LOS OLIVOS Y DICHA tabla no tiene primary key,es decir no esta anexada a ninguna otra tabla,pero en las demas tablas que trabajo,,coloque una columna llamada ubigeo que es un char(6) y seria algo asi ubigeo 150000 150117 Lo que quiero es crear una funcion,para que me reconoxca los nombres de los ubigeos usando como parametro el ubigeo char(6), asi poder mostrarlo en alguna Vista en la cual crea conveniente,estaba haciendo este codigo,pero me bota el sgte error: Msg 116, Level 16, State 1, Procedure obtenerDpto, Line 10 Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. Msg 116, Level 16, State 1, Procedure obtenerDpto, Line 12 Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. AQUI LES DEJO LA FUNCION create function obtenerDpto(@ubigeo char(6)) returns varchar(50) as begin declare @dep char(2) declare @prov char(2) declare @dist char(2) set @dep=(select substring(@ubigeo,1,2),(Select cod_dep , Nombre from Tb_Ubigeo where Cod_prov='00' and Cod_dist='00' and Cod_dep<>'00') from Tb_agente,Tb_Ubigeo where @dep=cod_dep and @ubigeo=ubigeo and @prov=cod_prov and @dist=cod_dist) return @dep return @prov return @dist end la tabla agente que aperece,es una de las tablas que posee la columa ubigeo,por ello esta en la sentencia anterior agradecere la ayuda que me puedan dar Título: Re: como leer mediante substring el codigo de un ubigeo en SQL Publicado por: pokareiser en 30 Marzo 2012, 18:05 pm dspues de analizarr logre esto:
create function obtenerdepartamento(@coddep char(2)) returns varchar(50) as begin declare @nombre varchar(50) set @nombre =(select nombre from tb_ubigeo where cod_dep=@coddep and cod_prov='00' and cod_dist='00') return @nombre end go alter function obtenerProvincia(@codprov char(4)) returns varchar(50) as begin declare @nombre varchar(50) set @nombre =(select nombre from Tb_ubigeo where cod_prov=right(@codprov,2) and cod_dep=LEFT(@codprov,2) and cod_dist='00') return @nombre end go distritoo kedara como tareaa............ |