Day 78 – MySQL CONCAT() Function
In MySQL, the CONCAT()
function is used to concatenate two or more strings into a single string. The basic syntax of the CONCAT()
function is as follows:
CONCAT(string1, string2, ...)
Here, string1
, string2
, etc. are the strings to concatenate. You can pass any number of strings as arguments to the CONCAT()
function, separated by commas.
For example, suppose we have a table called “students” with columns “first_name” and “last_name”. To concatenate the first and last names into a single column called “full_name”, we can use the following query:
SELECT CONCAT(first_name, ' ', last_name) AS full_name
FROM students;
This will return a result set with a single column called “full_name”, where each row contains the concatenated value of the “first_name” and “last_name” columns, separated by a space.
You can also use the CONCAT()
function with other MySQL string functions, such as UPPER()
or LOWER()
, to manipulate the input strings before concatenation. For example:
SELECT CONCAT(UPPER(first_name), ' ', LOWER(last_name)) AS full_name
FROM students;
This will return a result set where the “first_name” column is converted to uppercase and the “last_name” column is converted to lowercase before concatenation.