CONCAT_WS¶
The CONCAT_WS function in SQL concatenates two or more strings into one string with a separator.
Syntax¶
The syntax for the CONCAT_WS function in SQL is:
Parameters¶
separator: This is the string that will be placed between each string to be concatenated.string1,string2, ...,stringN: These are the strings that you wish to concatenate together.
Examples¶
Let's consider a few examples to understand how to use the CONCAT_WS function.
To concatenate strings with a comma separator:
This will return 'AB,CD,EF'.
You can also concatenate more than two strings:
This will return 'ab/cd/ef'.
The CONCAT_WS function will skip any NULL values:
This will return 'abcdef'.
The CONCAT_WS function can also take non-string arguments:
This will return '1234563.14'. In this case, the integers and float values are implicitly converted to strings before concatenation.
However, the CONCAT_WS function expects at least two arguments. If fewer than two arguments are passed to the CONCAT_WS function, it will throw an error:
This will throw an error because the CONCAT_WS function expects at least two arguments.