What is the difference between CONCAT vs CONCAT_WS in SQL

98 views Asked by At

Please clarify what information is pulled and how it is reorganized using CONCAT vs CONCAT_WS in SQL. See example below for more details.

For example,

CONCAT('Google', '.com') creates Google.com

but I don't understand how

CONCAT_WS ('.', 'www', 'google', 'com') also create Google .com

To me it seems like it would create .wwwgooglecom

Why is this not the case?

For example,

CONCAT('Google', '.com') creates Google.com

but I don't understand how

CONCAT_WS ('.', 'www', 'google', 'com') also create Google .com

To me it seems like it would create .wwwgooglecom

Why is this not the case?

2

There are 2 answers

2
Nikhil Patel On

CONCAT just combines the values provided as arguments as it is. For example - CONCAT('www','.google','.com') produces www.google.com

CONCAT_WS will combine the provided values based on the separator, which is the first argument. For example - CONCAT_WS('.', 'www','google','com') will produce www.google.com.

0
ne.romantic On

CONCAT_WS will handle NULL values as empty strings, whereas CONCAT will return NULL.