SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 big problem
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

adiboy0420
Starting Member

USA
10 Posts

Posted - 07/13/2012 :  01:07:41  Show Profile  Reply with Quote
how do i create a program which take two paramates,

first paramater is a varchar(5000), it will take a input string that is
seperated with a delimiter( but could be any delimiter)

second paramater is varchar(1)-which is the delimiter

ex

so if input '1,2,200,abc,xyz' --- so the delimiter is ','

so first paramater would be '1,2,200,abc,xyz'
and second parmater would be ','

answer needs to be
1
2
200
abc
xyz
,

but now also i can come back and put in the same program

so if input '1:2:200:abc:xyz' --- so the delimiter is ':'

so first paramater would be '1:2:200:abc:xyz'
and second parmater would be ':'

and answer needs to be now
1
2
200
abc
xyz
:


--------------------------------------------------------------------------------

how do i do that :(

------------ i have this so far


@a varchar(5000)
As

declare @x varchar(5000)
declare @y varchar (2000)
declare @z varchar(1000)

set @x =@a

set @y=@x+','

while(CHARINDEX(',',@y)>0)

begin

set @z= LEFT(@y,charindex(',',@y)-1)

select @z


set @y= RIGHT(@y,len(@y)-len(@z)-1)

end

but the code i have will only work with ','

so how i get it to pick up the if someone picks another delimiter

adiboy0420
Starting Member

USA
10 Posts

Posted - 07/13/2012 :  01:11:55  Show Profile  Reply with Quote
also you think i can just do if, else clause on this ?
Go to Top of Page

stepson
Yak Posting Veteran

Romania
85 Posts

Posted - 07/13/2012 :  01:50:47  Show Profile  Reply with Quote
Hi
try this


declare @str as varchar(5000)
declare @y as char(1)
set @y=','
--set @y=':'
set @str='1,2,200,abc,xyz'
--set @str='1:2:200:abc:xyz'
DECLARE @xml xml


SET @str = '<root><row>' + REPLACE(@str,@y,'</row><row>') + '</row><row>'+@y+'</row></root>'
SET @xml = CAST(@str AS XML)
SELECT tab.col.value ('.','varchar(50)')
FROM @xml.nodes('/root/row') tab(col)


S

Edited by - stepson on 07/13/2012 01:58:34
Go to Top of Page

nathans
Aged Yak Warrior

USA
933 Posts

Posted - 07/13/2012 :  02:08:20  Show Profile  Reply with Quote
Please see discussion on "Splitting Strings" here:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648

Go to Top of Page

adiboy0420
Starting Member

USA
10 Posts

Posted - 07/13/2012 :  10:23:10  Show Profile  Reply with Quote
thanks for the help stepson

but in the code, the use can put any of these

{
}
<
>
"
'
?
%
*
/
,


and it needs to run

so how should i do... should i repeat SELECT tab.col.value ('.','varchar(50)')
with all the delimiters?


-----------

nathans thanks.. i will read it
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29138 Posts

Posted - 07/13/2012 :  17:07:08  Show Profile  Visit SwePeso's Homepage  Reply with Quote
So... The first character not in the range of {a-z0-9} is the delimiter?
If this is true, you can write the function with autosense.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000