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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to get the field AFTER the comma

Author  Topic 

Kimi86
Yak Posting Veteran

79 Posts

Posted - 2012-06-14 : 04:37:49
I have a column that contains 2 fields which are comma delimited. How to get the field AFTER the comma.??
Example:
house,303 -> 303
Both the fields can have any number of characters but wont be more than 500 i guess..

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2012-06-14 : 04:43:30
Try this

select SUBSTRING('house,303',CHARINDEX(',','house,303')+1,LEN('house,303'))

Senthil Kumar C
------------------------------------------------------
MCITP - Database Administration SQL SERVER 2008
MCTS - Database Development SQL SERVER 2008
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-06-14 : 05:01:40
declare @testColumn varchar(255)
set @testColumn = 'house,303'
select parsename(replace(@testColumn,',','.'),1)



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-06-14 : 05:13:40
[code]
declare @testColumn varchar(255)
set @testColumn = 'house,303'
select right(@testColumn, charindex(',', reverse(@testColumn)) - 1)
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-06-14 : 05:16:03
or using SwePeso's fnParseString

select dbo.fnParseString(-2, ',', @testColumn),
dbo.fnParseString(1, ',', @testColumn)



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-06-14 : 05:20:48
or what sunita would like to do:

stuff(@testColumn,1,charindex(',',@testColumn),'')



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-06-14 : 05:40:47
and you should redesign your database as soon as possible to avoid this mess


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -