Display results as vertical string? Use PIVOT?

  • Hello all -

    This is probably very simple but the solution evades me. I have a single column returned from a select statement. How can I have this returned as a vertical string? I looked into using PIVOT but my scenario seems too simple to use Pivot. I'm not requiring any aggregate functions or anything.

    So, I want to turn this:

    SELECT CountyNames + ',' FROM Counties

    ---------------------

    County1 ,

    County2 ,

    County3 ,

    ..etc

    TO:

    County1, County2, County3...etc

    Please note that the list of rows is unknown. Could be 1. Could be 50.

    Thanks in advance!!

  • declare @myCSV varchar(1000)

    set @myCSV=''

    select @myCSV=@myCSV+','+ name from sys.databases

    select substring(@myCSV, 2, len(@myCSV)-1) as myCSV

  • If I understand correctly, this article explains what you need:

    http://www.sqlservercentral.com/articles/comma+separated+list/71700/

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply