We Recommend

SQL Cookbook SQL Cookbook
Written in O'Reilly's popular Problem/Solution/Discussion style, the SQL Cookbook is sure to please. Anthony's credo is: "When it comes down to it, we all go to work, we all have bills to pay, and we all want to go home at a reasonable time and enjoy what's still available of our days." The SQL Cookbook moves quickly from problem to solution, saving you time each step of the way.


Posted By

mafro on 07/25/08


Tagged


Versions (?)


Set Comma Separated List


Published in: SQL 


Build a comma-separated list of ids.

  1. declare @site_ids varchar(1000)
  2. SET @site_ids = ''
  3.  
  4. --concat'd list of site_ids
  5. SELECT @site_ids = @site_ids + ', ' + convert(varchar,site_id)
  6. FROM tbl_site
  7.  
  8. --remove starting and trailing commas
  9. SET @site_ids = substring(@site_ids, 3, len(@site_ids)-3)
  10.  
  11. print @site_ids

Report this snippet 

You need to login to post a comment.