I use SQL Server 2012
I have the following problem, in a table there are records of municipalities and two respective UFs, however the need arises to divide this column into two municipalities and one UF, but the data is like this:
CANDÓI-PR
VERÊ-PR
NAO-ME-TOQUE-RS
NAO-ME-TOQUE / RS
SERTAO / RS
JABOTICABA/RS
VERÊ
So if someone can create a code that separates this in 2 and doing an UPDATE in the original, taking into account that there is no default in the column eg there is '/' '-' and there are some without UFs that should return NULL
I tried the following code:
select
case when CHARINDEX('-',[MUNICIPIO / UF])>0
then SUBSTRING([MUNICIPIO / UF],1,CHARINDEX('-',[MUNICIPIO / UF])-1)
else [MUNICIPIO / UF] end Municipio,
CASE WHEN CHARINDEX('-',[MUNICIPIO / UF])>0
THEN SUBSTRING([MUNICIPIO / UF],CHARINDEX('-',[MUNICIPIO / UF])+1,len([MUNICIPIO / UF]))
ELSE NULL END as UF
from TABLE
UNION all
select
case when CHARINDEX('/',[MUNICIPIO / UF])>0
then SUBSTRING([MUNICIPIO / UF],1,CHARINDEX('/',[MUNICIPIO / UF])-1)
else [MUNICIPIO / UF] end Municipio,
CASE WHEN CHARINDEX('/',[MUNICIPIO / UF])>0
THEN SUBSTRING([MUNICIPIO / UF],CHARINDEX('/',[MUNICIPIO / UF])+1,len([MUNICIPIO / UF]))
ELSE NULL END as UF
from TABLE
They work separately but together they return duplicate values and the distinct does not help because there really are equal values in the table.