Wednesday, 15 February 2012

SQL Server: Best way to concatenate multiple columns? -



SQL Server: Best way to concatenate multiple columns? -

i trying concatenate multiple columns in query in sql server 11.00.3393.

i tried new function concat() it's not working when utilize more 2 columns.

so wonder if that's best way solve problem:

select concat(concat(concat(column1,column2),column3),column4) mytable

i can't utilize column1 + column2 because of null values.

edit

if seek select concat('1','2','3') result error

the concat function requires 2 argument(s)

through discourse it's clear problem lies in using vs2010 write query, uses canonical concat() function limited 2 parameters. there's way alter that, i'm not aware of it.

an alternative:

select '1'+'2'+'3'

this approach requires non-string values cast/converted strings, null handling via isnull() or coalesce():

select isnull(cast(col1 varchar(50)),'') + coalesce(convert(varchar(50),col2),'')

sql sql-server-2012

No comments:

Post a Comment