SQL Server: Concatenate Strings with Separator

We've already learned how to concatenate multiple strings using (+) and the CONCAT function. Sometimes we want to concatenate two or more strings together with a separator, such as a comma delimited text.

e.g.

Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday --> comma delimiter character

Monday/Tuesday/Wednesday/Thursday/Friday/Saturday/Sunday --> slash delimiter character


Actually, we can just repeatedly insert each delimiter between the 2 strings we want to concatenate, but there is a more efficient way to use the CONCAT_WS function.

Fungsi CONCAT_WS() menambahkan dua atau lebih teks string dengan pemisah.

Syntax:

CONCAT_WS(separator, string1, string2, ...., string_n)


e.g.

SELECT CONCAT_WS(', ', 'Monday', 'Tuesday', 'Wednesday'
                 'Thursday', 'Friday', 'Saturday', 'Sunday')

SELECT CONCAT_WS('/ ''Monday''Tuesday''Wednesday'
                 'Thursday''Friday''Saturday''Sunday')







Post a Comment

0 Comments