NULL safe drop in replacement approximations for SQL Server 2012 CONCAT function
SQL Server 2012:
SELECT CONCAT(data1, data2)
PRE SQL 2012 (Two Solutions):
SELECT {fn CONCAT(ISNULL(data1, ''), ISNULL(data2, ''))}
SELECT ISNULL(CAST(data1 AS varchar(MAX)), '') + ISNULL(CAST(data2 AS varchar(MAX)), '')
These two solutions collate several excellent answers and caveats raised by other posters including @Martin Smith, @Svish and @vasin1987.
These options add NULL
to ''
(empty string) casting for safe NULL
handling while accounting for the varying behaviour of the +
operator pertaining to specific operands.
Note the ODBC Scaler Function solution is limited to 2 arguments whereas the +
operator approach is scalable to many arguments as needed.
Note also the potential issue identified by @Swifty regarding the default varchar
size here remedied by varchar(MAX)
.
CONCAT
is new for 2012.