July 27, 2005 at 1:36 am
Hi
I need to display the list of rows in CSV formated result set
Eg.
MyTable
ColA ColB
1 AAAA
2 BBBB
3 CCCC
4 DDDD
I need to display the as 1, 2, 3, 4 . I don't need ColB for this.
Thanks in Advance
Sathish
S A T ...
Sathish's Blog
July 27, 2005 at 1:48 am
Use DTS for this, it is the easiets way.
Hope this helps...
Ford Fairlane
Rock and Roll Detective
July 27, 2005 at 8:09 am
SET NOCOUNT ON
DECLARE @MyTable TABLE
(
ColA INT,
ColB VARCHAR(10)
)
INSERT INTO @MyTable VALUES (1,'AAAA')
INSERT INTO @MyTable VALUES (2,'BBBB')
INSERT INTO @MyTable VALUES (3,'CCCC')
INSERT INTO @MyTable VALUES (4,'DDDD')
INSERT INTO @MyTable VALUES (5,'EEEE')
/* RESULT */
DECLARE @Result VARCHAR(100)
SELECT @Result = COALESCE(@Result + ', ', '') + CONVERT(VARCHAR, ColA)
FROM
@MyTable
SELECT @Result Result
Regards,
gova
July 27, 2005 at 8:19 am
I like that one...
But what if the row order changes?
SET NOCOUNT ON
DECLARE @MyTable TABLE
(
ColA INT,
ColB VARCHAR(10)
)
INSERT INTO @MyTable VALUES (1,'AAAA')
INSERT INTO @MyTable VALUES (2,'BBBB')
INSERT INTO @MyTable VALUES (4,'DDDD')
INSERT INTO @MyTable VALUES (5,'EEEE')
INSERT INTO @MyTable VALUES (3,'CCCC')
/* RESULT */
DECLARE @Result VARCHAR(100)
SELECT @Result = COALESCE(@Result + ', ', '') + CONVERT(VARCHAR, ColA)
FROM
@MyTable
SELECT @Result Result
This returns
, 1, 2, 4, 5, 3
and you (probably) still want 1, 2, 3, 4, 5 back. Can you manage that (without putting a clustered index on colA)?
July 27, 2005 at 8:27 am
SELECT @Result = COALESCE(@Result + ', ', '') + CONVERT(VARCHAR, ColA)
FROM
(SELECT TOP 100 PERCENT * FROM @MyTable ORDER BY 1) A
Regards,
gova
July 27, 2005 at 8:30 am
Very nice, I'm impressed - I will store this trick for later use
July 27, 2005 at 3:18 pm
The same result:
SELECT @Result = COALESCE(@Result + ', ', '') + CONVERT(VARCHAR, ColA)
FROM @MyTable
ORDER BY ColA
July 27, 2005 at 10:29 pm
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply