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
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Query help. Need to Remove Comma at the end.
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

NguyenL71
Posting Yak Master

USA
205 Posts

Posted - 11/14/2013 :  12:40:13  Show Profile  Reply with Quote

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

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 11/14/2013 :  13:22:58  Show Profile  Reply with Quote

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

SELECT STUFF((SELECT ',' + HIXID FROM @TempGroup ORDER BY CAST(HIXID  AS bigint) FOR XML PATH('')),1,1,'')


output
--------------------------------
120000000000001,150000000000007,220100000000002,220100000000003



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

NguyenL71
Posting Yak Master

USA
205 Posts

Posted - 11/14/2013 :  14:10:51  Show Profile  Reply with Quote

visakhm,
Thank you so much. I try similar logic before on my end but it did not work but your work.

Again, thank you.



quote:
Originally posted by visakh16


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

SELECT STUFF((SELECT ',' + HIXID FROM @TempGroup ORDER BY CAST(HIXID  AS bigint) FOR XML PATH('')),1,1,'')


output
--------------------------------
120000000000001,150000000000007,220100000000002,220100000000003



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs


Go to Top of Page

Rajan Sahai
Starting Member

India
8 Posts

Posted - 11/23/2013 :  01:59:37  Show Profile  Reply with Quote
unspammed
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.05 seconds. Powered By: Snitz Forums 2000