0% encontró este documento útil (0 votos)
38 vistas11 páginas

SQL Dividir Direcciones

Cargado por

26patrivargas
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como PDF, TXT o lee en línea desde Scribd
0% encontró este documento útil (0 votos)
38 vistas11 páginas

SQL Dividir Direcciones

Cargado por

26patrivargas
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como PDF, TXT o lee en línea desde Scribd

use prueba2

/************* Script para divider direcciones *******************/

go

SELECT Direccion,identificacion,ROW_NUMBER() OVER(PARTITION BY Direccion

ORDER BY Direccion)Numero, VALUE as Descripcion

FROM CLIENTE

CROSS APPLY STRING_SPLIT((REPLACE(REPLACE(Direccion,'#',''),'-','')),' ')

SELECT Direccion,identificacion,ROW_NUMBER() OVER(PARTITION BY Direccion

ORDER BY Direccion)Numero, VALUE as Descripcion INTO ##Direccion

FROM CLIENTE

CROSS APPLY STRING_SPLIT((REPLACE(REPLACE(Direccion,'#',''),'-','')),' ')

SELECT distinct identificacion,Direccion,

(SELECT Descripcion FROM ##Direccion WHERE identificacion=[Link] and


Numero=1)Dir1,

(SELECT Descripcion FROM ##Direccion WHERE identificacion=[Link] and


Numero=2)Dir2,

(SELECT Descripcion FROM ##Direccion WHERE identificacion=[Link] and


Numero=3)Dir3,

(SELECT Descripcion FROM ##Direccion WHERE identificacion=[Link] and


Numero=4)Dir4,

(SELECT Descripcion FROM ##Direccion WHERE identificacion=[Link] and


Numero=5)Dir5,

(SELECT Descripcion FROM ##Direccion WHERE identificacion=[Link] and


Numero=6)Dir6,

(SELECT Descripcion FROM ##Direccion WHERE identificacion=[Link] and


Numero=7)Dir7,
(SELECT Descripcion FROM ##Direccion WHERE identificacion=[Link] and
Numero=8)Dir8,

(SELECT Descripcion FROM ##Direccion WHERE identificacion=[Link] and


Numero=9)Dir9,

(SELECT Descripcion FROM ##Direccion WHERE identificacion=[Link] and


Numero=10)Dir10

INTO ##Direccion2

FROM ##Direccion d

commit

DECLARE @DIR VARCHAR(10),@Nit VARCHAR(10)

BEGIN TRAN

BEGIN TRY

IF (SELECT COUNT(Dir3) FROM ##Direccion2 WHERE DIR3 like '[1-9]%')>0

BEGIN

SELECT @DIR=Dir3,@Nit=identificacion FROM ##Direccion2 WHERE DIR3 like '[1-9]%'

UPDATE Direccion SET Num_Viagen=@DIR

FROM ##Direccion2 D WHERE [Link]=@Nit;

END

ELSE

IF (SELECT COUNT(Dir3) FROM ##Direccion2 WHERE DIR3 like '[A-Z]%')>0

BEGIN

SELECT @DIR=Dir3,@Nit=identificacion FROM ##Direccion2 WHERE DIR3 like '[A-Z]%'

UPDATE Direccion SET Let_ViaPr=@DIR

FROM ##Direccion2 D WHERE [Link]=@Nit

END

/*** ****/

---DECLARE @DIR VARCHAR(10),@Nit VARCHAR(10)

IF (SELECT count(Dir4) FROM ##Direccion2 WHERE Dir4 like '[1-9]%')>0


BEGIN

SELECT @DIR=Dir4,@Nit=identificacion FROM ##Direccion2 WHERE Dir4 like '[1-9]%'

UPDATE Direccion SET Num_Viagen=@DIR

FROM ##Direccion2 D WHERE [Link]=@Nit

END

ELSE

IF (SELECT count(Dir4) FROM ##Direccion2 WHERE Dir4 like '[A-Z]%')>0

BEGIN

SELECT @DIR=Dir4,@Nit=identificacion FROM ##Direccion2 WHERE Dir4 like '[A-Z]%'

UPDATE Direccion SET Ide_Sector=@DIR

FROM ##Direccion2 D WHERE [Link]=@Nit

END

/***** ****/

IF (SELECT count(Dir5) FROM ##Direccion2 WHERE Dir5 like '[1-9]%')>0

BEGIN

SELECT @DIR=Dir5,@Nit=identificacion FROM ##Direccion2 WHERE Dir5 like '[1-9]%'

UPDATE Direccion SET Num_Viagen=@DIR

FROM ##Direccion2 D WHERE [Link]=@Nit

END

ELSE

IF (SELECT count(Dir5) FROM ##Direccion2 WHERE Dir5 like '[A-Z]%')>0

BEGIN

SELECT @DIR=Dir5,@Nit=identificacion FROM ##Direccion2 WHERE Dir5 like '[A-Z]%'

UPDATE Direccion SET Let_Viagen=@DIR

FROM ##Direccion2 D WHERE [Link]=@Nit

END
/*** ****/

IF (SELECT count(Dir6) FROM ##Direccion2 WHERE Dir6 like '[1-9]%')>0

BEGIN

SELECT @DIR=Dir6,@Nit=identificacion FROM ##Direccion2 WHERE Dir6 like '[1-9]%'

UPDATE Direccion SET Num_Predio=@DIR

FROM ##Direccion2 D WHERE [Link]=@Nit

END

ELSE

IF (SELECT count(Dir6) FROM ##Direccion2 WHERE Dir6 like '[A-Z]%')>0

BEGIN

SELECT @DIR=Dir6,@Nit=identificacion FROM ##Direccion2 WHERE Dir6 like '[A-Z]%'

UPDATE Direccion SET Let_Viagen=@DIR

FROM ##Direccion2 D WHERE [Link]=@Nit

END

/*** ****/

IF (SELECT count(Dir7) FROM ##Direccion2 WHERE Dir7 like '[1-9]%')>0

BEGIN

SELECT @DIR=Dir7,@Nit=identificacion FROM ##Direccion2 WHERE Dir7 like '[1-9]%'

UPDATE Direccion SET Num_Predio=@DIR

FROM ##Direccion2 D WHERE [Link]=@Nit

END

ELSE

IF (SELECT count(Dir7) FROM ##Direccion2 WHERE Dir7 like '[A-Z]%')>0

BEGIN

SELECT @DIR=Dir7,@Nit=identificacion FROM ##Direccion2 WHERE Dir7 like '[A-Z]%'

UPDATE Direccion SET Otr_Caract=(@DIR+Dir8+Dir9+Dir10)


FROM ##Direccion2 D WHERE [Link]=@Nit

END

/**** ***/

IF (SELECT Dir8 FROM ##Direccion2 WHERE Dir8 is not null)>0

BEGIN

SELECT @DIR=Dir8,@Nit=identificacion FROM ##Direccion2 WHERE Dir8 is not null

UPDATE Direccion SET Otr_Caract=(@DIR+Dir9+Dir10)

FROM ##Direccion2 D WHERE [Link]=@Nit

END

COMMIT

END TRY

BEGIN CATCH

ROLLBACK

END CATCH

SELECT * FROM ##Direccion2

SELECT * FROM Direccion

USE prueba2

GO

/***

SELECT Direccion,identificacion,ROW_NUMBER() OVER(PARTITION BY Direccion

ORDER BY Direccion)Numero, VALUE as Descripcion


FROM CLIENTE

CROSS APPLY STRING_SPLIT((REPLACE(REPLACE(Direccion,'#',''),'-','')),' ')

SELECT Direccion,identificacion,ROW_NUMBER() OVER(PARTITION BY Direccion

ORDER BY Direccion)Numero, VALUE as Descripcion INTO ##Direccion

FROM CLIENTE

CROSS APPLY STRING_SPLIT((REPLACE(REPLACE(Direccion,'#',''),'-','')),' ')

SELECT distinct identificacion,Direccion,

(SELECT Descripcion FROM ##Direccion WHERE identificacion=[Link] and


Numero=1)Dir1,

(SELECT Descripcion FROM ##Direccion WHERE identificacion=[Link] and


Numero=2)Dir2,

(SELECT Descripcion FROM ##Direccion WHERE identificacion=[Link] and


Numero=3)Dir3,

(SELECT Descripcion FROM ##Direccion WHERE identificacion=[Link] and


Numero=4)Dir4,

(SELECT Descripcion FROM ##Direccion WHERE identificacion=[Link] and


Numero=5)Dir5,

(SELECT Descripcion FROM ##Direccion WHERE identificacion=[Link] and


Numero=6)Dir6,

(SELECT Descripcion FROM ##Direccion WHERE identificacion=[Link] and


Numero=7)Dir7,

(SELECT Descripcion FROM ##Direccion WHERE identificacion=[Link] and


Numero=8)Dir8,

(SELECT Descripcion FROM ##Direccion WHERE identificacion=[Link] and


Numero=9)Dir9,

(SELECT Descripcion FROM ##Direccion WHERE identificacion=[Link] and


Numero=10)Dir10

INTO ##Direccion2
FROM ##Direccion d

****/

DECLARE @DIR VARCHAR(10),@Nit VARCHAR(10), @DIR4 VARCHAR(10), @DIR5


VARCHAR(10), @DIR6 VARCHAR(10),

@DIR7 VARCHAR(10), @DIR8 VARCHAR(10)

/**

UPDATE Direccion SET


Let_ViaPr='',Ide_Sector='',Num_Viagen='',Let_Viagen='',Num_Predio='',Otr_Caract=''

FROM ##Direccion2 d where [Link]=[Link] ***/

---SELECT TOP 0 identificacion INTO ##Direccion3 FROM ##Direccion2

--TRUNCATE TABLE ##Direccion3

--UPDATE Direccion SET Abr_ViaPr=d.DIR1, Num_ViaPr=d.DIR2 FROM ##Direccion2 d WHERE


[Link]=[Link]

BEGIN TRAN

WHILE (SELECT COUNT(*) FROM ##Direccion2 D LEFT JOIN ##Direccion3 T ON


[Link]=[Link] WHERE [Link] IS NULL)>0

BEGIN

BEGIN TRY

SELECT TOP 1 @Nit=identificacion FROM ##Direccion2 WHERE identificacion NOT IN


(SELECT identificacion FROM ##Direccion3 )

Insert into ##Direccion3 values (@Nit)

IF (SELECT COUNT(Dir3) FROM ##Direccion2 WHERE DIR3 like '[0-9]%' and


identificacion=@Nit)>0

BEGIN

SELECT @DIR=Dir3 FROM ##Direccion2 WHERE DIR3 like '[0-9]%' and identificacion=@Nit

UPDATE Direccion SET Num_Viagen=@DIR

FROM ##Direccion2 D WHERE [Link]=@Nit;

END

ELSE

IF (SELECT COUNT(Dir3) FROM ##Direccion2 WHERE DIR3 like '[A-Z]%' and


identificacion=@Nit)>0
BEGIN

SELECT @DIR=Dir3 FROM ##Direccion2 WHERE DIR3 like '[A-Z]%' and identificacion=@Nit

UPDATE Direccion SET Let_ViaPr=@DIR

FROM ##Direccion2 D WHERE [Link]=@Nit

END

/*** ****/

IF (SELECT count(Dir4) FROM ##Direccion2 WHERE Dir4 like '[0-9]%' and


identificacion=@Nit)>0

BEGIN

SELECT @DIR4=Dir4 FROM ##Direccion2 WHERE Dir4 like '[0-9]%' and identificacion=@Nit

UPDATE Direccion SET Num_Viagen=@DIR4

FROM ##Direccion2 D WHERE [Link]=@Nit

END

ELSE

IF (SELECT count(Dir4) FROM ##Direccion2 WHERE Dir4 like '[A-Z]%' and


identificacion=@Nit)>0

BEGIN

SELECT @DIR4=Dir4 FROM ##Direccion2 WHERE Dir4 like '[A-Z]%' and identificacion=@Nit

UPDATE Direccion SET Ide_Sector=@DIR4

FROM ##Direccion2 D WHERE [Link]=@Nit

END

/***** ****/

IF (SELECT count(Dir5) FROM ##Direccion2 WHERE Dir5 like '[0-9]%' and


identificacion=@Nit)>0

BEGIN

SELECT @DIR5=Dir5 FROM ##Direccion2 WHERE Dir5 like '[0-9]%' and identificacion=@Nit
UPDATE Direccion SET Num_Viagen=@DIR5

FROM ##Direccion2 D WHERE [Link]=@Nit

END

ELSE

IF (SELECT count(Dir5) FROM ##Direccion2 WHERE Dir5 like '[A-Z]%' and


identificacion=@Nit)>0

BEGIN

SELECT @DIR5=Dir5 FROM ##Direccion2 WHERE Dir5 like '[A-Z]%' and identificacion=@Nit

UPDATE Direccion SET Let_Viagen=@DIR5

FROM ##Direccion2 D WHERE [Link]=@Nit

END

/*** ****/

IF (SELECT count(Dir6) FROM ##Direccion2 WHERE Dir6 like '[0-9]%' and


identificacion=@Nit)>0

BEGIN

SELECT @DIR6=Dir6 FROM ##Direccion2 WHERE Dir6 like '[0-9]%' and identificacion=@Nit

UPDATE Direccion SET Num_Predio=@DIR6

FROM ##Direccion2 D WHERE [Link]=@Nit

END

ELSE

IF (SELECT count(Dir6) FROM ##Direccion2 WHERE Dir6 like '[A-Z]%' and


identificacion=@Nit)>0

BEGIN

SELECT @DIR6=Dir6 FROM ##Direccion2 WHERE Dir6 like '[A-Z]%' and identificacion=@Nit

UPDATE Direccion SET Let_Viagen=@DIR6

FROM ##Direccion2 D WHERE [Link]=@Nit

END
/*** ****/

IF (SELECT count(Dir7) FROM ##Direccion2 WHERE Dir7 like '[0-9]%' and


identificacion=@Nit)>0

BEGIN

SELECT @DIR7=Dir7 FROM ##Direccion2 WHERE Dir7 like '[0-9]%' and identificacion=@Nit

UPDATE Direccion SET Num_Predio=@DIR7

FROM ##Direccion2 D WHERE [Link]=@Nit

END

ELSE

IF (SELECT count(Dir7) FROM ##Direccion2 WHERE Dir7 like '[A-Z]%' and


identificacion=@Nit)>0

BEGIN

SELECT @DIR7=Dir7 FROM ##Direccion2 WHERE Dir7 like '[A-Z]%' and identificacion=@Nit

UPDATE Direccion SET Otr_Caract=(@DIR7+ ' ' +ISNULL(Dir8,'')+ ' ' +ISNULL(Dir9,'')+ ' ' +
ISNULL(Dir10,''))

FROM ##Direccion2 D WHERE [Link]=@Nit

END

/**** ***/

IF (SELECT count(Dir8) FROM ##Direccion2 WHERE Dir8 is not null and


identificacion=@Nit)>0

BEGIN

SELECT @DIR8=Dir8 FROM ##Direccion2 WHERE Dir8 is not null and identificacion=@Nit

UPDATE Direccion SET Otr_Caract=(@DIR8+ ' ' + ISNULL(Dir9,'')+ ' ' +ISNULL(Dir10,''))

FROM ##Direccion2 D WHERE [Link]=@Nit

END

COMMIT

END TRY
BEGIN CATCH

ROLLBACK

END CATCH

END

---SELECT * FROM Direccion

---SELECT Dir8 FROM ##Direccion2 WHERE Dir8 is not null and

---SELECT * FROM ##Direccion2

--DROP TABLE ##Direccion3

---select * from ##Direccion3

---UPDATE Direccion SET


Let_ViaPr='',Ide_Sector='',Num_Viagen='',Let_Viagen='',Num_Predio='',Otr_Caract=''

También podría gustarte