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
 General SQL Server Forums
 New to SQL Server Programming
 Split function help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

tsaliki
Starting Member

India
19 Posts

Posted - 10/09/2012 :  08:17:55  Show Profile  Reply with Quote
have a table like below:

create table testjob

(

jobid int,
jobname varchar(100),
time float,
name varchar(50)
)

insert into testjob values ( 1001,'java work',4.5,'arjun')
insert into testjob values ( 1005,'sql work',10,'arjun')
insert into testjob values ( 1010,'.net work',7.5,'arjun')
insert into testjob values ( 1040,'java work',5.5,'ravi')
insert into testjob values ( 1023,'php work',2.5,'arjun')
insert into testjob values ( 1027,'.net work',3.5,'ravi')


i have a query as below:


select jobname from testjob where jobid in (select items from dbo.split('1001,1005',','))

the output is as below:

Jobname

Javawork
sql work

Instead i want the output beside like this : i.e i want it side by side.

Jobname

Javawork,sqlwork

sql-programmers
Posting Yak Master

USA
190 Posts

Posted - 10/09/2012 :  08:40:20  Show Profile  Visit sql-programmers's Homepage  Reply with Quote
Hi Try This,

SELECT (SELECT CAST(a.jobname AS VARCHAR) + ','
FROM testjob a WHERE a.jobid in (select items from dbo.split('1001,1005',','))
FOR
XML PATH('') ) AS jobname




SQL Server Programmers and Consultants
http://www.sql-programmers.com/
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 10/09/2012 :  08:42:12  Show Profile  Reply with Quote

;With cte as (SELECT jobname from testjob where jobid in (select items from dbo.split('1001,1005',',')))
SELECT STUFF(
(SELECT ',' + c.jobname
FROM cte c
FOR XML PATH('')),1,1,'') AS CSV


--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 10/09/2012 :  23:17:18  Show Profile  Reply with Quote
quote:
Originally posted by sql-programmers

Hi Try This,

SELECT (SELECT CAST(a.jobname AS VARCHAR(100)) + ','
FROM testjob a WHERE a.jobid in (select items from dbo.split('1001,1005',','))
FOR
XML PATH('') ) AS jobname




SQL Server Programmers and Consultants
http://www.sql-programmers.com/


Always specify a length while casting to character datatype

see

http://visakhm.blogspot.com/2010/02/importance-of-specifying-length-in.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

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.09 seconds. Powered By: Snitz Forums 2000