SQL CONCAT Vs CONCAT_WS Function

The distinction between CONCAT and CONCAT_WS functions will be discussed in this article.


SQL CONCAT Function

When doing concatenation, use the CONCAT() function to efficiently handle NULL data.

CONCAT combines two or more strings into a single string.

Example: One argument

Sql server,Mysql server

SELECT CONCAT('SQL', ' is', ' query language');

Sqlite

SELECT('SQL' ||' '|| 'is'|| ' ' || 'query language');

MS access

SELECT("SQL " & "is " & "query language");

Output: The output above query is

SQL is query language


SQL CONCAT_WS Function

The MySQL CONCAT WS() method joins two or more strings separated by a separator.

Between two strings, the separator supplied in the first parameter is appended. A string can be used as the separator.

The result is NULL if the separator is NULL.

WS stands for "with separator."

Example: With a separator ", ", the following MySQL statement adds the first and second arguments.

Sql-server,Mysql

SELECT CONCAT_WS(',','1st string','2nd string');

Output:

1st string,2nd string

SQL Difference BETWEEN CONCAT AND CONCAT_WS Function

There are 3 main contrast between concat vs concat_ws:-

CONCAT CONCAT_WS
There is no concept of a separator in the CONCAT() function. The CONCAT WS() function can concatenate strings and include a separator.
If any of the arguments is NULL, the CONCAT() method returns NULL. If the separator is NULL, the CONCAT WS() method returns NULL.
Multiple input columns are concatenated into a single column with concat. Concatenates multiple input string columns into a single string column using CONCAT WS().