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
 select multiple rows in one column with comma
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

anaze
Starting Member

Indonesia
6 Posts

Posted - 07/26/2013 :  15:48:00  Show Profile  Reply with Quote
CREATE TABLE [dbo].[x1](
[nomer] [varchar](15) NOT NULL,
[tgl] [datetime] NOT NULL,
CONSTRAINT [pk_x1] PRIMARY KEY CLUSTERED
(
[nomer] ASC,
[tgl] asc
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

insert into x1 values ('1','2013-07-30')
insert into x1 values ('1','2013-07-29')
insert into x1 values ('1','2013-07-28')

select * from x1
Result :
nomer tanggal
1 2013-07-28 00:00:00.000
1 2013-07-29 00:00:00.000
1 2013-07-30 00:00:00.000

I Want to make :
nomer tanggal
1 28,29,30

Any Idea...?
Please help me

James K
Flowing Fount of Yak Knowledge

3649 Posts

Posted - 07/26/2013 :  15:58:38  Show Profile  Reply with Quote
You can use the XML PATH method like shown below
SELECT
	x1.nomer,
	STUFF(x2.tanggal,1,1,'') AS tanggal
FROM
	(SELECT DISTINCT nomer FROM x1) AS x1
	CROSS APPLY
	(
		SELECT ','+CAST(DAY([tgl]) AS VARCHAR(2)) FROM 
		x1 x2 WHERE x2.nomer = x1.nomer
		FOR XML PATH('')
	) x2(tanggal);


And this is a blatant and shameless attempt to plug a blog:

------------------------------------------------------------------------------------------------------
SQL Server SSB
http://myshallowsqlblog.wordpress.com/the-lowdown-on-that-xml-path-thingie/
Go to Top of Page

anaze
Starting Member

Indonesia
6 Posts

Posted - 07/26/2013 :  16:26:18  Show Profile  Reply with Quote
Oh nice...
Thanks a lot Mr. James K
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.03 seconds. Powered By: Snitz Forums 2000