For your data sample you can do, change the dynamic table to your actual table, create the fields in your table and try to make the change.
You can use a Begin tran
and in case it’s all right commit
or rollback
declare @Enderecos table
(
Enderecos varchar(max),
TipodeLogradouro varchar(200),
Logradouro varchar(200),
Numero varchar(200)
)
insert into @Enderecos(Enderecos) values
('Rua Antonio José Nerchis, 81'),
('Rua: Guaira/JD Pacera, 1201'),
('Rua 321, N 168'),
('R ALTAIR GAGLIARDI, 132'),
('Rua Rio Grande do Sul, 243'),
('Rua Duque de Caxias'),
('R CAPELLEN, 58'),
('ROD ACESSO PLINIO ARLINDO DE NEZ, 4303'),
('R Anibal Gazaniga, 107'),
('R D PEDRO I, 231'),
('Rua Dep. Ivan Ferreira do Amaral, 440'),
('R SANTA CATARINA, 711'),
('Rua Santa Catarina, 411'),
('Rua Augusto de Jesus, 77'),
('Rua Marcondes Sobrinho, 40'),
('rua Antonio Tonelli nº 755 casa 2')
update @Enderecos
set TipodeLogradouro = REPLACE(REPLACE(REPLACE(LEFT(Enderecos, charindex(' ', Enderecos) - 1),',' , ''),':' , ''),';' , ''),
Numero =
case when Enderecos like '%º%' then substring(Enderecos, charindex('º', Enderecos) - 1, 100 )
else
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace
(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace
(reverse(left(reverse(Enderecos), charindex(' ', reverse(Enderecos)) -1)
) COLLATE sql_latin1_general_cp1251_ci_as, 'z', ''), 'x', ''), 'w', ''), 'y', ''), 'v', ''), 'u', ''), 't', ''), 's', ''), 'r', ''), 'q', ''), 'p', '')
, 'o', ''), 'n', ''), 'm', ''), 'l', ''), 'k', ''), 'j', ''), 'i', ''), 'h', '')
, 'g', ''), 'f', ''), 'e', ''), 'd', ''), 'c', ''), 'b', ''), 'a', ''),',' , ''),':' , ''),';' , ''),' ',''),'º',''),'.',''),'/',''),'\','')
end
from @Enderecos
update @Enderecos
set Numero = REPLACE(Numero,'N?','Nº')
update @Enderecos
set Logradouro = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(Enderecos,TipodeLogradouro,''),Numero,'')
,':' , ''),';' , ''),',',''),'Nº',''),'.',''),'\','')
select * from @Enderecos
changed to have a given ('rua Antonio Tonelli nº 755 casa 2')
complement.
you can put a sample of your data, all are separated by , comma ?
– Marco Souza
It may be possible depending on whether your data follows some kind of pattern. Provide a few more data examples to identify some pattern.
– Guilherme Lautert
@Guilhermelautert Standard...ta hard to find, but follow a few more examples... I will put in question more examples.
– Isa
@GOKUSSJ4 added real examples in the question.
– Isa
If inputs are opened to the user it can be difficult to define an expression for all cases, as the user can fill in as desired.
– gmsantos
Not being able to notice anything outside that the number is always at the end, yet as @gmsantos commented it would become very problematic to try to deal with all cases. Not to mention you can’t tell if
R
is street or highway. It would be more useful to change the system and inform the customer so that they update the data.– Guilherme Lautert
I agree with @Guilhermelautert. This would be the solution with better reliability
– gmsantos
@Guilhermelautert understood your position, in fact, I was in the same situation when I tried to solve this problem... Unfortunately this option to update the data is unfeasible, since it is an internal registration and that the responsible for it would have to enter in many registers (more or less 1900 records...). If we create the following pattern, it always starts with "Street" and ends with the number, as would the regular expression?
– Isa