Showing posts with label Comma separated field values. Show all posts
Showing posts with label Comma separated field values. Show all posts

Tuesday, August 25, 2009

Comma separated column values in sqlserver

If there is any requirement arieses we need to show comma separated column values eg:
Dummy values (Requirement)
abc,xyz,pqr etc.
Database column values
fields have an id
CatId CatName
1              abc
2              xyz
3              pqr

To achieve above requirement use following code:
DECLARE @Cat varchar(2000)Select @Cat = COALESCE(@Cat + ', ', '') + CAST(SubCategoryName AS varchar(200))FROM SubCategoryWHERE CategoryId=@CategoryIDReturn @Cat