Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 2525 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > SQL Server > Get all the values of a single column in a variable ...
Lakhangarg

Get all the values of a single column in a variable

 Code Snippet posted by: Lakhangarg | Posted on: 8/30/2009 | Category: SQL Server Codes | Views: 1860 | Status: [Member] [Moderator] | Alert Moderator   


Get all the values of a single column in a variable separated by comma.
DECLARE a Variable and set the Size of the variable according to your requirement. here in this code i have mentioned MAX.

Now Using

SELECT  @TempString =  @TempString + CityName +',' FROM Tbl_City


Statement we can append all the values in @TempString separated by comma.
@TempString will contain a extra comma at the end. so we need to remove that with the help of

SUBSTRING(@TempString,0,LEN(@TempString))


DECLARE @TempString VARCHAR(MAX)

SET @TempString = ''

SELECT @TempString = @TempString + CityName +',' FROM Tbl_City

SELECT SUBSTRING(@TempString,0,LEN(@TempString))

Found interesting? Add this to:


 Responses

Pandians
Posted by: Pandians | Posted on: 8/30/2009 | Level: Silver | Status: [Member] [MVP] | Alert Moderator 

Hi

Nice effort.

We have two more options...
1.COALESCE
2.XQuery

Cheers


Cheers
www.SQLServerbuddy.blogspot.com
iLink Multitech Solutions

Lakhangarg
Posted by: Lakhangarg | Posted on: 8/30/2009 | Level: Silver | Status: [Member] [Moderator] | Alert Moderator 

Hi Pandians-

Thanks for adding valuable information..


>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/19/2013 2:50:09 AM