trying to extract fulladdress to individual column such as address1 address2 City State Zip, and each of the fulladdress has a comma
so that why I am trying to create a function that count the comma and put the fulladdress ito individual column.
hear is the script that I am making but i got stuck at city
alter function Address(@fulladdress varchar(100),@section varchar(35))
returns varchar(100)
as
begin
declare @desiredadd varchar (100)
declare @lenght int
declare @i int
declare @comma1 int
declare @comma2 int
declare @comma3 int
declare @comma4 int
--set @desiredadd = @fulladdress
set @i = 1
set @lenght = len(@fulladdress)
--while (@i <= @length)
--begin
if @section ='add1' begin
set @comma1 = charindex (',',@fulladdress,1)
set @desiredadd=substring(@fulladdress,1,@comma1-1)
end
else if @section ='add2' begin
set @comma1 = charindex (',',@fulladdress,1)
set @desiredadd=substring(@fulladdress,@comma1+1,len(@fulladdress))
set @comma2 = charindex (',',@desiredadd,1)
set @desiredadd=substring(@desiredadd,1,@comma2-1)
end
else if @section ='city' begin
set @comma1 = charindex (',',@fulladdress,1)
set @desiredadd=substring(@fulladdress,@comma1+1,len(@fulladdress))
set @comma2 = charindex (',',@desiredadd,1)
set @desiredadd=substring(@fulladdress,1,@comma2-1)
set @comma3 = charindex (',',@desiredadd,1)
set @desiredadd=substring(@desiredadd,1,@comma3-1)
end
return @desiredadd
end