Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

All records in one field 2

Status
Not open for further replies.

christer99

IS-IT--Management
Dec 3, 2001
247
I need to get the results of the query below into one record (and not multiple rows). The query below produces the following rows/results:
1
2
4
5
8

Using a SQL select statement, can I get the result to be '1, 2, 4, 5, 8' I will not ahead of time how many records or what is the range, but we are talking fewer than 20 records as the max.


SELECT BRCH
FROM INS
WHERE (BRCH IS NOT NULL)
GROUP BY BRCH
ORDER BY BRCH
 
Code:
[COLOR=blue]Declare[/color] @Output [COLOR=blue]VarChar[/color](8000)
[COLOR=blue]Set[/color] @Output = [COLOR=red]''[/color]

[COLOR=blue]SELECT[/color] @Output = @Output + [COLOR=#FF00FF]Convert[/color]([COLOR=blue]VarChar[/color](20), BRCH) + [COLOR=red]','[/color]
[COLOR=blue]FROM[/color]   INS
[COLOR=blue]WHERE[/color]  (BRCH [COLOR=blue]IS[/color] NOT NULL)
[COLOR=blue]ORDER[/color] [COLOR=blue]BY[/color] BRCH

[COLOR=blue]If[/color] [COLOR=#FF00FF]Right[/color](@Output, 1) = [COLOR=red]','[/color]
  [COLOR=blue]Set[/color] @Output = [COLOR=#FF00FF]Left[/color](@Output, Len(@Output)-1)

[COLOR=blue]Select[/color] @Output

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top