Author |
Topic |
ssunny
Posting Yak Master
133 Posts |
Posted - 2008-12-19 : 12:38:31
|
Hello Guys,Need help with string parser. Let's say here's my input string:abcd,abcd,abcd,abcdAs you can see a string has 3 commas (,). What I want to find out is string before last comma (,) . So in this case my output should be abcd abcd abcdIf input string is abcd abcd,abcd then output should be abcd abcdHere's what I wrote:declare @input varchar (500),@output varchar(500),@checkno intset @input = 'abcd,abcd,abcd,abcd'set @checkno = 0select @checkno = len (@input) - len (replace(@input,',',''))--print @checknoif @checkno = 1begin select @output = substring(@input,1,charindex(',',@input,1)-1)endelse begin (having problem in writing this part)endselect @outputThanks in advance. |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-12-20 : 10:06:41
|
[code]declare @input varchar (500),@output varchar(500),@checkno intset @input = 'aaaa,bbbb,cccc,dddd'set @checkno = 0select @checkno = len (@input) - len (replace(@input,',',''))--print @checknoif @checkno = 1beginselect @output = substring(@input,1,charindex(',',@input,1)-1)endelse begin--(having problem in writing this part)select @output=parsename(replace(@input,',','.'),4)+' 'select @output=@output+parsename(replace(@input,',','.'),3)+' 'select @output=@output+parsename(replace(@input,',','.'),2)endselect @output[/code]Webfred No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
ssunny
Posting Yak Master
133 Posts |
Posted - 2008-12-22 : 11:29:45
|
quote: Originally posted by webfred
declare @input varchar (500),@output varchar(500),@checkno intset @input = 'aaaa,bbbb,cccc,dddd'set @checkno = 0select @checkno = len (@input) - len (replace(@input,',',''))--print @checknoif @checkno = 1beginselect @output = substring(@input,1,charindex(',',@input,1)-1)endelse begin--(having problem in writing this part)select @output=parsename(replace(@input,',','.'),4)+' 'select @output=@output+parsename(replace(@input,',','.'),3)+' 'select @output=@output+parsename(replace(@input,',','.'),2)endselect @output Webfred No, you're never too old to Yak'n'Roll if you're too young to die.
Hey Webfred,Thanks for the reply!But your code is not working for input string'aaaa bbbb, cccc, dddd'. For this string I should get 'aaaa bbbb cccc' but I'm getting null.Here's what I came up with :declare @input varchar (500),@output varchar(500),@checkno int,@temp_input varchar(500),@temp_input1 varchar(500)set @input = 'aaaa bbbb, cccc, dddd'select @temp_input = reverse (@input)select @checkno = charindex (',',@temp_input,1)select @temp_input1 = substring (@temp_input,@checkno + 1,len(@temp_input))select @temp_input1 = reverse(@temp_input1)select @temp_input1 = replace (@temp_input1,',','')select @temp_input1Thanks. |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-12-22 : 14:04:00
|
Sorry - i have corrected it:declare @input varchar (500),@output varchar(500),@checkno int---set @input = 'aaaa,bbbb,cccc,dddd'set @input = 'aaaa bbbb, cccc, dddd'set @checkno = 0select @checkno = len (@input) - len (replace(@input,',',''))--print @checknoif @checkno = 1beginselect @output = substring(@input,1,charindex(',',@input,1)-1)endelse begin--(having problem in writing this part)select @output=isnull(parsename(replace(@input,',','.'),4)+' ','')select @output=@output+isnull(parsename(replace(@input,',','.'),3)+' ','')select @output=@output+isnull(parsename(replace(@input,',','.'),2),'')endselect @output Webfred No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
ssunny
Posting Yak Master
133 Posts |
Posted - 2008-12-22 : 14:49:41
|
quote: Originally posted by webfred Sorry - i have corrected it:declare @input varchar (500),@output varchar(500),@checkno int---set @input = 'aaaa,bbbb,cccc,dddd'set @input = 'aaaa bbbb, cccc, dddd'set @checkno = 0select @checkno = len (@input) - len (replace(@input,',',''))--print @checknoif @checkno = 1beginselect @output = substring(@input,1,charindex(',',@input,1)-1)endelse begin--(having problem in writing this part)select @output=isnull(parsename(replace(@input,',','.'),4)+' ','')select @output=@output+isnull(parsename(replace(@input,',','.'),3)+' ','')select @output=@output+isnull(parsename(replace(@input,',','.'),2),'')endselect @output Webfred No, you're never too old to Yak'n'Roll if you're too young to die.
This works great! Thanks a lot Webfred!! |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-12-22 : 15:11:09
|
Welcome No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
|
|
|