Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
paxmaster
Starting Member
4 Posts |
Posted - 2009-01-16 : 14:49:41
|
| I have two tables one table (developerteam1) has only two columns FullName and FullAddress and Other table (developerteamexpanded) has FirstName LastName AddressLine1 AddressLine2 City State Zip I was creating a function but only add1 and add2 works other like City State and zip don't work; hear is the functionalter function Address(@fulladdress varchar(100),@section varchar(35))returns varchar(100)asbegindeclare @desiredadd varchar (100)declare @lenght intdeclare @i intdeclare @comma1 intdeclare @comma2 intdeclare @comma3 intdeclare @comma4 int--set @desiredadd = @fulladdressset @i = 1set @lenght = len(@fulladdress)--while (@i <= @length)--beginif @section ='add1' beginset @comma1 = charindex (',',@fulladdress,1)set @desiredadd=substring(@fulladdress,1,@comma1-1)endelse if @section ='add2' beginset @comma1 = charindex (',',@fulladdress,1)set @desiredadd=substring(@fulladdress,@comma1+1,len(@fulladdress))set @comma2 = charindex (',',@desiredadd,1)set @desiredadd=substring(@desiredadd,1,@comma2-1)endelse if @section ='city' beginset @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)endelse if @section ='state' beginset @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(@fulladdress,1,@comma3-1)set @comma4 = charindex (',',@desiredadd,1)set @desiredadd=substring(@desiredadd,1,@comma4-1)endreturn @desiredaddend----then on I have to create a store proc and use this function to insert values into other table (developerteamexpanded) |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-01-16 : 15:32:50
|
| You didn't say why your function is not working. Have you searched this site for similar topics? (ie: "split address", "city state zip", etc...)One challenge will be when one or more address sections are missing from the full address. You'll need to define some rules for those possibilities. Are there guaranteed delimiters between the sections? That will make it much easier. Post a good representation of the possible values.Similar issue with full name, There can be prefixes, suffixes, middle names, middle initials, multi part names, etc.Be One with the OptimizerTG |
 |
|
|
paxmaster
Starting Member
4 Posts |
Posted - 2009-01-16 : 16:07:49
|
| yeah I figure out the FullName Columns But I am concern with FullAddress I have tested the add1 and add2 with this script select dbo.address(fulladdress, 'add1')from DeveloperTeam1 select dbo.address(fulladdress, 'add2')from DeveloperTeam1 it works but when I try to do City it gives blanks or null yeah I search the web but no luck that what i am looking for let me you an example of a row on developerteam1 INSERT INTO DeveloperTeam1 VALUES ('Albert Butt', '122 Street, Apt No 65, Jersey City, NJ, 07406'As you see there is comma when there is a comma will be a columns Thanks for quick reply TGPaxmaster |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
|
|
paxmaster
Starting Member
4 Posts |
Posted - 2009-01-17 : 02:25:24
|
| I have already had look at http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648it does not help because I am doing total different thingI have to insert the value of split form table 1 to table 2 by useing store sproc select * from dbo.Split(N'paxmaster,TG,Zack', it will give me:paxmasterTGzackThat's not what i wantThe Whole columns |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-17 : 02:48:35
|
wont this be enough?INSERT INTO developerteamexpandedSELECT FirstName,LastName,Add1,Add2,City,State,ZipFROM(SELECT d1.FullName,MAX(CASE WHEN n.ID=1 THEN n.Val ELSE NULL END) AS FirstName,MAX(CASE WHEN n.ID=2 THEN n.Val ELSE NULL END) AS LastNameFROM developerteam1 d1CROSS APPLY dbo.ParseValues(d1.FullName,',')nGROUP BY d1.FullName)nameINNER JOIN(SELECT d2.FullName,MAX(CASE WHEN a.ID=1 THEN a.Val ELSE NULL END) AS Add1,MAX(CASE WHEN a.ID=2 THEN a.Val ELSE NULL END) AS Add2,MAX(CASE WHEN a.ID=3 THEN a.Val ELSE NULL END) AS City,MAX(CASE WHEN a.ID=4 THEN a.Val ELSE NULL END) AS State,MAX(CASE WHEN a.ID=5 THEN a.Val ELSE NULL END) AS ZipFROM developerteam1 d2CROSS APPLY dbo.ParseValues(d1.FullAddress,',')aGROUP BY d2.FullName)addressON address.FullName=name.FullName ParseValues can be found herehttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=115544 |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-01-17 : 08:38:32
|
quote: Originally posted by paxmaster I have already had look at http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648it does not help because I am doing total different thingI have to insert the value of split form table 1 to table 2 by useing store sproc select * from dbo.Split(N'paxmaster,TG,Zack', it will give me:paxmasterTGzackThat's not what i wantThe Whole columns
I was not attempting to provide you with a complete solution. You seemed to be having a problem simply seperating out a comma seperated list of values. These functions are proven methods to do just that. Once you have them parsed I assumed you had the ability/imagination to complete your specific task(s) on your own. If I over estimated you, I appologize. Be One with the OptimizerTG |
 |
|
|
|
|
|
|
|