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 2005 Forums
 Transact-SQL (2005)
 split function

Author  Topic 

harun
Starting Member

1 Post

Posted - 2009-08-29 : 01:08:11
Hi all,
my tbl as follows.
id city

1 chennai
2 delhi
3 mumbai
.... .....
if am passing the string like='1,2,3'.it has to return the string as chennai,delhi,mumbai(concatenation)..pls help me to fix it.

Thx in advance..

Marshy
Starting Member

2 Posts

Posted - 2009-08-29 : 04:10:47
Hi,

You can try something like this (City would be the table holding your entries):

---
DECLARE
@str varchar(100)
,@x XML

SET @str = '1,2,3'
SET @x = '<i>' + REPLACE(@str, ',', '</i><i>') + '</i>'

SELECT STUFF(REPLACE(
(
SELECT ','+[City] AS 'text()'
FROM
(
SELECT City.*
FROM
(
SELECT x.i.value('.', 'INT') AS ID
FROM @x.nodes('//i') x(i)
) A
INNER JOIN City ON A.ID = City.ID
) B
FOR XML PATH(''))
,' ,',','),1,1,'')


---

Added some changes based on posts below
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-08-29 : 05:24:26
select stuff((select ','+ city from tablename where cast(id as varchar(max)) like '%'+ @values+'%'
for xml path('')),1,1,'')
Go to Top of Page

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2009-08-29 : 05:47:42
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648
Go to Top of Page
   

- Advertisement -