Thursday, March 29, 2012

Concatenate a string in SQL

I have a char(10) column in a table, I would like to concatenate the string
to a variable with coma's in between the strings. I currently use a cursor.
Can any one think of a better way?
--
Thanks,
Jon AHere is a technique:
declare @.myVar varchar(1000)
set @.myVar = ''
select @.myVar = @.myVar + ', ' + myColumn from myTable
set @.myVar = substring(@.myVar,3,1000)
print @.myVar
"Jon A" wrote:

> I have a char(10) column in a table, I would like to concatenate the strin
g
> to a variable with coma's in between the strings. I currently use a cursor
.
> Can any one think of a better way?
> --
> Thanks,
> Jon A|||Here is example from http://vyaskn.tripod.com/code/cat.txt
USE pubs
GO
DECLARE @.title_ids varchar(150), @.delimiter char
SET @.delimiter = ','
SELECT @.title_ids = COALESCE(@.title_ids + @.delimiter, '') + title_id FROM
titles
SELECT @.title_ids AS [List of Title IDs]
Ana
"Jon A" wrote:

> I have a char(10) column in a table, I would like to concatenate the strin
g
> to a variable with coma's in between the strings. I currently use a cursor
.
> Can any one think of a better way?
> --
> Thanks,
> Jon A|||If you mean each column value frpm each row in the the table in a single
comma-delimited string, then no, afaik there is no way without row=based
processing. But a cursor is not the only way... You could use a while loop.
.
Declare @.Out VarChar(8000) Set @.Out = ''
Declare @.ColVal Char(10) Set @.ColVal =''
While exists (Select * From Table
Where ColName > @.ColVal)
Select @.ColVal = Min(ColName),
@.Out = @.Out + RTrim(Min(ColName)) + ','
From Table
Where ColName > @.ColVal
[Untested]
the above approach has the property that it will alphabeticize the entries
in the output tng, and ensure that duplicates are only entered once.
Or, if the Table has a primary key, and you want the duplicates entered
individually,
you can put all the PK values into a table variable and cycle through that
table, delting the records as you use them... This is equivilent to using a
cursor, and much faster...
Declare @.Out VarChar(8000) Set @.Out = ''
Declare @.Val VarChar(10)
Declare @.PK Integer
Declare @.PKs Table (PK Integer Primary Key Not Null)
Insert @.PKs(PK) Select PKColumn From TableName
While exists (Select * From @.PKs) Begin
Select @.PK = Min(PK) From @.PKs
Select @.Out = @.Out + ColName + ','
From Table Wher e PKCol = @.PK
Delete @.PKs Where PK = @.PK
End
hth,
Charly
"Jon A" wrote:

> I have a char(10) column in a table, I would like to concatenate the strin
g
> to a variable with coma's in between the strings. I currently use a cursor
.
> Can any one think of a better way?
> --
> Thanks,
> Jon A|||> I have a char(10) column in a table, I would like to concatenate the string
> to a variable with coma's in between the strings. I currently use a cursor
.
> Can any one think of a better way?
Yes! A better way is to handle presentation and formatting on the client
(that's kind of why it's called the presentation layer).
But of course, like many other inadvisable things, there are kludges to do
this in a query:
http://www.aspfaq.com/2529

No comments:

Post a Comment