Jon Visconti
Jon Visconti

Reputation: 17

TSQL concatenation of multiple columns

I need to concatenate 7 columns from a table into a single column called scan_data.

I know there is a concatenate option in SQL, but I'm not sure how to implement it.

Upvotes: 0

Views: 67

Answers (1)

Kai
Kai

Reputation: 2158

For Sql Server 2012 or newer, just use the Concat function:

select scan_data = concat(column1, column2, column3, column4, column5, column6, column7)
from MyTable

For versions before Sql Server 2012, you'll need to use:

select scan_data = column1 + column2 + column3 + column4 + column5 + column6 + column7
from MyTable

If any of your columns contains null values, you'll need to null-guard them (ie isnull(column1, '')) or the entire concatenated string will return null. Only the pre-2012 version requires null-guarding - concat takes care of that for you.

Upvotes: 2

Related Questions