Headder AdSence

SQL SERVER – CONCAT function and NULL values

CONCAT is the new T-SQL function introduced in SQL Server 2012. It is used to concatenate the values. It accepts many parameter values seperated by comma. All parameter values are concatenated to a single string.
A simple example is
SELECT CONCAT('SQL Server',' 2012')
which results to SQL Server 2012
The same can be done using + in the earlier versions
SELECT 'SQL Server'+' 2012'
which results to SQL Server 2012
But did you know the advantage of CONCAT function over +?
SELECT 'SQL Server'+' 2012'+NULL
When you execute the above, the result is NULL
But the CONCAT function will simply ignore NULL values
SELECT CONCAT('SQL Server',' 2012',NULL)
The result is SQL Server 2012
So by using CONCAT function, you do not need to worry about handling NULL values.

No comments:

Post a Comment