CONCAT() Function SQL (+)
To connect multiple strings together is called "concatenation". To do this in mySQL the CONCAT() function is employed with the various strings to be combined as its parameters. For example:
SELECT CONCAT(book,', by ', author) AS bna FROM fict_chars
We apply this to our female fictional characters table, "fict_chars".
| fem_id | name | book | author |
|---|---|---|---|
| 1 | Scarlet O'Hara | Gone With the Wind | Margaret Mitchell |
| 2 | Hermione Granger | Harry Potter | J.K. Rowling |
| 3 | Jane Porter | Tarzan | E.R. Burroughs |
The result:
| bna |
|---|
| Gone With the Wind, by Margaret Mitchell |
| Harry Potter, by J.K. Rowling |
| Tarzan, by E.R. Burroughs |
In other applications of SQL the + operator may be used to the same effect. Our example above would look like:
SELECT (book + ,', by ', + author) AS bna FROM fict_chars
The CONCAT_WS() function is a special form of the CONCAT() function. The WS stands for "with separator". The syntax:
SELECT CONCAT_WS('separator', 'string' or column, 'string' or column, etc.)
It will concatenate strings with a designated symbol between each string.
< COALESCE FUNCTION SQL | COUNT Function SQL >
