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
 General SQL Server Forums
 New to SQL Server Programming
 Query help. Need to Remove Comma at the end.

Author  Topic 

NguyenL71
Posting Yak Master

228 Posts

Posted - 2013-11-14 : 12:34:58
It looks simple but I try different way and can't get this to work, I have a list of id concat them and remove the last comma at the
end. Please see the results desire below. Thank you so much. SQL 2012


DECLARE @TempGroup TABLE
(
CounterId INT IDENTITY(1,1) NOT NULL,
HIXID VARCHAR(50) NULL
);

INSERT @TempGroup
SELECT 220100000000002
UNION
SELECT 220100000000003
UNION
SELECT 120000000000001
UNION
SELECT 150000000000007

DECLARE @List VARCHAR(8000)
SET @List = ''

SELECT @List = (@List + HIXID + ', ')
FROM @TempGroup
PRINT @List

--Result want:
120000000000001, 150000000000007, 220100000000002, 220100000000003

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-11-14 : 13:10:30
You can make a slight tweak then you don't need to remove a comma:
 SELECT @List = (@List + CASE WHEN @List <> '' THEN ', ' ELSE '' END + HIXID) 
FROM @TempGroup
PRINT @List
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-14 : 13:23:32
Duplicate of

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=189654

please dot open multiple threads for same issue

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -