MySQL CONCAT Function: String Concatenation

The CONCAT() function in MySQL is used to concatenate two or more strings into one string.

Prerequisites:

  • A MySQL server up and running
  • Access to a MySQL user account with privileges to query tables

Tutorial:

  • Connect to the MySQL server:

To start the mysql command-line client, open a terminal or command prompt, and enter:

mysql -u [username] -p

Replace [username] with your MySQL username and enter your password when prompted.

  • Select a database:

Select the database containing the table you want to query:

USE [database_name];

Replace [database_name] with the name of your database.

  • Use the CONCAT() function with constants:

You can use the CONCAT() function with constant string values:

SELECT CONCAT('Hello', ' ', 'World');

This will return a single string: 'Hello World'.

  • Use the CONCAT() function with table columns:

Suppose you have a users table with the following columns: id, first_name, last_name. You can use the CONCAT() function to combine the first_name and last_name columns:

SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM users;

This query will return a column full_name which combines the first_name and last_name with a space in between.

  • Use the CONCAT() function with NULL:

If any of the arguments to the CONCAT() function is NULL, the function returns NULL. You can use the CONCAT_WS() function instead if you want to treat NULL values as empty strings:

SELECT CONCAT('Hello', NULL, 'World');
-- This will return NULL

SELECT CONCAT_WS(' ', 'Hello', NULL, 'World');
-- This will return 'Hello World'
  • Exit the MySQL command-line client:
EXIT;

Now you have learned how to use the MySQL CONCAT() function to concatenate strings. This is a useful function when you need to combine text data in your SQL queries.

  1. How to use CONCAT function in MySQL:

    • CONCAT is used to concatenate two or more strings together.
      SELECT CONCAT('Hello', ' ', 'World') AS result;
      
  2. Concatenating strings in MySQL with CONCAT:

    • Concatenate strings using CONCAT.
      SELECT CONCAT('First', 'Second') AS result;
      
  3. MySQL CONCAT with multiple strings:

    • Concatenate multiple strings using CONCAT.
      SELECT CONCAT('One', ' ', 'Two', ' ', 'Three') AS result;
      
  4. Concatenation using CONCAT_WS in MySQL:

    • CONCAT_WS allows specifying a separator between concatenated strings.
      SELECT CONCAT_WS(', ', 'Apple', 'Banana', 'Orange') AS result;
      
  5. Examples of using CONCAT in MySQL queries:

    • Use CONCAT in SELECT queries for string concatenation.
      SELECT CONCAT(column1, ' - ', column2) AS result FROM your_table;
      
  6. Concatenating columns with CONCAT in MySQL:

    • Concatenate columns using CONCAT.
      SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees;
      
  7. Concatenating numbers and strings in MySQL:

    • Concatenate numbers and strings together.
      SELECT CONCAT('ID: ', employee_id) AS employee_info FROM employees;
      
  8. Handling NULL values with CONCAT in MySQL:

    • CONCAT automatically handles NULL values by treating them as empty strings.
      SELECT CONCAT('First Name: ', first_name, ' Last Name: ', COALESCE(last_name, '')) AS full_name FROM users;
      
  9. Using CONCAT in WHERE clause in MySQL:

    • Apply CONCAT in the WHERE clause for conditional filtering.
      SELECT * FROM your_table WHERE CONCAT(column1, ' ', column2) = 'target_value';
      
  10. Concatenating with separators in MySQL:

    • Use CONCAT with separators for better formatting.
      SELECT CONCAT(first_name, ', ', last_name) AS full_name FROM employees;
      
  11. Concatenating date and time values in MySQL:

    • Concatenate date and time values using CONCAT.
      SELECT CONCAT('Date: ', DATE_FORMAT(date_column, '%Y-%m-%d'), ' Time: ', TIME_FORMAT(time_column, '%H:%i:%s')) AS datetime_info FROM your_table;