How to concatenate two columns that might contain NULL values in a select statement?

TSR

I tried with:

SELECT CONCAT(col1, col2) AS NewCol FROM tableName

It works only if col1 and col2 are not null, else NewCol becomes null, even if one of col1 and col2 is not null. I want it to display whatever value that is available.

Vamsi Prabhala

Use CONCAT_WS.

SELECT CONCAT_WS('', col1, col2) AS NewCol FROM tableName

It would return an empty string in case both the values are null.

Documentation:

CONCAT_WS(separator,str1,str2,...)

CONCAT_WS() stands for Concatenate With Separator and is a special form of CONCAT(). The first argument is the separator for the rest of the arguments. The separator is added between the strings to be concatenated. The separator can be a string, as can the rest of the arguments. If the separator is NULL, the result is NULL.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to concatenate columns in a Postgres SELECT?

Fetching JSON which might contain null values

How to use row values as columns in a SELECT statement in PostgreSQL?

How to check a column values is null or not in select statement

Concatenate multiple columns, with null values

How to compare values between two columns with custom logic IF statement in MySQL

How might I concatenate all values in a row into a string?

How to Concatenate Two Extracted Columns?

How to concatenate two strings in SQL statement?

Concatenate two columns in SELECT query and get MAX

Concatenate two columns of spark dataframe with null values

Concatenate all values between two columns

How to select records with maximum values in two columns?

How to replace any NULL values with an integer that might or could appear in the Foreign key columns

How do you concatenate two integer values using an update statement in SQL

SELECT Statement with NULL and Non-Null Values

How to join two tables on two columns normally, but if one of columns contain null, then result must contain rows that match another column only?

Select all rows where two columns contain a combination of values

How To Insert 2 Values in 2 columns and rest null values in rest columns without using Create Table Statement

How to concatenate columns in update statement

How to concatenate two values from different columns into a single column

How to concatenate two columns of spark dataframe with null values but get one value

How to concatenate two different values from two different columns with comma " , " using TSQL?

concatenate two columns values pandas

How to concatenate two columns in oracle select query?

How to concatenate string columns that contain NaN values?

How to sort a list of dictionaries by a list that might contain duplicate values?

Polars and the Lazy API: How to drop columns that contain only null values?

Jolt concatenate only if my two columns are not Null, default value if it is NULL