Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concatenate null value columns in Tsql

I use + to concatenate several columns's value. But + doesnt work if one of that columns has null value. For example

Select null+ 'Test'

query returns null instead of 'Test'.

What are your advices to solve that problem?

like image 500
jhash Avatar asked Apr 18 '11 11:04

jhash


People also ask

How do I concatenate columns with NULL values in SQL?

Concatenating Data When There Are NULL Values To resolve the NULL values in string concatenation, we can use the ISNULL() function. In the below query, the ISNULL() function checks an individual column and if it is NULL, it replaces it with a space. We can verify the output of the data as shown below.

Can we concatenate string with NULL?

To concatenate null to a string, use the + operator. Let's say the following is our string. String str = "Demo Text"; We will now see how to effortlessly concatenate null to string.

Can NULL values be joined in SQL?

The result of a join of null with any other value is null. Because null values represent unknown or inapplicable values, Transact-SQL has no basis to match one unknown value to another. You can detect the presence of null values in a column from one of the tables being joined only by using an outer join.


1 Answers

On versions prior to SQL Server 2012 you should use

   Select ISNULL(YourColumn,'') + 'Test' /*Or COALESCE(YourColumn,'')*/

to avoid this issue.

There is a connection option SET CONCAT_NULL_YIELDS_NULL OFF but that is deprecated.

SQL Server 2012 introduces the CONCAT function that treats NULL as an empty string when concatenating.

SELECT CONCAT(null,'Test')
like image 187
Martin Smith Avatar answered Oct 23 '22 11:10

Martin Smith