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)
 values seperating by comma

Author  Topic 

khusiie
Yak Posting Veteran

78 Posts

Posted - 2009-02-13 : 13:05:10
Hi folks,

there is one query:
select date, originid, originname, [origin desc], [origin desc other],origintype from origindept

i have one origindept tablename and there is fieldname origin desc, and origin desc other so i want the first value to go in origin desc and rest all will be go in origin desc other field...

example:

queen creek elemenrary school- school district
Miss Annapolis Outstanding Teen
Union Avenue Y
Nevada Regional Medical Center
Fifth Street Missionary Baptist Church Health/Wellness-Computer Technology Initiative
Hope Community Ministries, Inc.
University of Kentucky Cooperative Extension
United Way Greater High Point
Healthsmart

so first value will go in
origin desc
--------------
queen creek elemenrary school- school district

and rest will go in

origin desc other
---------------------
Miss Annapolis Outstanding Teen,Union Avenue Y,Nevada Regional ,Medical Center,Fifth Street Missionary Baptist Church ,Health/Wellness-Computer Technology Initiative ,Hope Community Ministries Inc.,University of Kentucky Cooperative Extension,United Way Greater High Point,Healthsmart

can anyone sort it out this?please..or give any suggestion.

thanks for ur help..

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-13 : 13:07:48
so originid will be same for both?
Go to Top of Page

khusiie
Yak Posting Veteran

78 Posts

Posted - 2009-02-13 : 13:09:04
yes originid is same for both
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-13 : 13:30:12
which field will have this whole value from where you need to extract to other two columns?
queen creek elemenrary school- school district
Miss Annapolis Outstanding Teen
Union Avenue Y
Nevada Regional Medical Center
Fifth Street Missionary Baptist Church Health/Wellness-Computer Technology Initiative
Hope Community Ministries, Inc.
University of Kentucky Cooperative Extension
United Way Greater High Point
Healthsmart


Go to Top of Page

khusiie
Yak Posting Veteran

78 Posts

Posted - 2009-02-13 : 13:40:13
its in [origin desc] column
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-13 : 13:42:42
so do you mean cross tabbing into single record per originid from multiple records based on collapsingvalues of [origin desc] field?
Go to Top of Page

khusiie
Yak Posting Veteran

78 Posts

Posted - 2009-02-13 : 13:47:08
i think so!! as i want the values vertically..so..yes!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-13 : 13:53:17
seems like this

;With CTE (Seq,date, originid, originname, [origin desc], [origin desc other],origintype)
AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY originid ORDER BY date),
date, originid, originname, [origin desc], [origin desc other],origintype
from origindept
)

SELECT c.date, c.originid, c.originname, c.[origin desc], LEFT(t.u,LEN(t.u)-1),c.origintype
FROM CTE c
OUTER APPLY (SELECT [origin desc] + ','
FROM CTE
WHERE originid = c.originid
AND Seq>=2
FOR XML PATH('')) t(u)
WHERE c.Seq=1
Go to Top of Page
   

- Advertisement -