The CONCAT function is useful for combining (aka concatenating) 2 or more strings into a single string. It can be super practical when you want to create custom text values by merging data from multiple columns or adding some static texts.
Let's go through some example so that it makes more sense:
Let's say we have a table called customers with columns first_name and last_name.
| first_name | last_name |
|---|---|
| John | Smith |
| Taylor | Swift |
| Jonathan | Donald |
| Mickey | Mouse |
And you want to create a full name column, that will have first name spaced and last name. Here is what you will need to write in SQL to make it happen using the CONCAT function:
SELECT
first_name,
last_name,
CONCAT(first_name, ' ', last_name) AS full_name
FROM customers
The concat function is basically combining strings together. In our case above, it's stitching together first_name, space indicated by ' ' and last_name.
You also create your custom URL website with CONCAT. Let's say each user want to create a website that will be displayed in this format, taking Dwayne Johnson as an example:
www.Dwayne-Johnson.com
We basically have the "www." first then their first name then dash "-" then their last name and we finish with ".com. Let's make that happen in SQL:
SELECT
CONCAT('www.', first_name, '-', last_name, '.com')
FROM customers
Let's get better with CONCAT with some practice!