MySQL

Chapter 7 - DQL (Data Query Language)

CONCAT

The CONCAT function in MySQL is part of the Data Query Language (DQL) and is used to combine two or more strings into a single string. This function is highly useful when you want to merge values from different columns or add additional text to a result set. It is commonly used in reports, displaying full names, and formatting output that combines data from multiple fields.

Here’s how to use the CONCAT function, along with examples and useful tips for new students:

  1. Basic Syntax of CONCAT:

    • The CONCAT function can combine two or more strings or column values into a single result.
    • Syntax:
    SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees;
    • This query combines the first_name and last_name columns into a full_name column, with a space between the two names.
  2. Concatenating Multiple Columns:

    • You can concatenate more than two columns or values by simply adding them to the CONCAT function.
    • Example:
    SELECT CONCAT(first_name, ' ', last_name, ' - ', job_title) AS employee_info FROM employees;
    • This will return a string that combines the employee's first name, last name, and job title, with appropriate spaces and dashes for clarity.
  3. Using CONCAT with Static Text:

    • You can include static text within the CONCAT function to format your output.
    • Example:
    SELECT CONCAT('Employee: ', first_name, ' ', last_name) AS employee_label FROM employees;
    • This query prepends the string "Employee: " before the full name of each employee.
  4. Handling NULL Values in CONCAT:

    • If any of the values passed to CONCAT is NULL, the entire result will be NULL. To avoid this, you can use COALESCE to replace NULL with an empty string.
    • Example:
    SELECT CONCAT(COALESCE(first_name, ''), ' ', COALESCE(last_name, '')) AS full_name FROM employees;
    • This query ensures that if first_name or last_name is NULL, it will be treated as an empty string, preventing the entire result from becoming NULL.
  5. Combining CONCAT with Other Functions:

    • You can use CONCAT in combination with other SQL functions like UPPER() or LOWER() to format the concatenated string.
    • Example:
    SELECT CONCAT(UPPER(first_name), ' ', LOWER(last_name)) AS formatted_name FROM employees;
    • This query converts the first_name to uppercase and the last_name to lowercase in the final concatenated string.
  6. Using CONCAT_WS for Custom Delimiters:

    • If you want to concatenate values with a specific delimiter, you can use CONCAT_WS (Concatenate With Separator) instead of CONCAT.
    • Example:
    SELECT CONCAT_WS('-', first_name, last_name, department_id) AS employee_info FROM employees;
    • This query combines the first name, last name, and department ID with a hyphen (-) separating the values.
  7. Performance Considerations:

    • Using CONCAT is efficient for string concatenation, but make sure to handle large datasets carefully, especially if performing complex concatenations or including many columns.

The CONCAT function is a versatile tool for combining values in MySQL queries. Whether you’re formatting output, displaying full names, or merging various data points, CONCAT can help you organize and present your data more effectively.

Tansy SQL Course | CONCAT | Chapter 7 | Lesson 7 - Video Thumbnail

Test code

Combine the first name and last name to create the full name of the employee.

--MySQL
SELECT
    employee_id, employee_number,
    concat(first_name , '  ' , last_name) as employee_full_name
FROM org_employee;

-- Postgres, Oracle and MS SQL
SELECT
    employee_id, employee_number,
    first_name || ' ' || last_name as employee_full_name
FROM org_employee;
Try it now

SQL CONCAT, combine multiple columns

Example 1 - Raw data from employee table

i

Example 1 - CONCAT employee first and last name

SELECT employee_id, employee_number, concat(first_name, ' ', last_name) as employee_full_name FROM org_employee;

Example 1 - Query data mapping

i

The contents of the first_name and last_name columns will be merged to create a unified column, separated by a space.

Example 1 - Query Output

i

Example 2 - Raw data from client table

i

Example 2 - CONCAT client name and address

SELECT client_id, first_name || ' ' || last_name as client_full_name, address1 || ', ' || city || ', ' || state || ' ' || postal_code as client_address FROM org_client;

Example 2 - Query data mapping

i

The first_name and last_name columns will be amalgamated to generate a consolidated column named 'client full name', with a space as the separator. Subsequently, the address, city, state, and zip will be amalgamated to produce the client's address.

Example 2 - Query Output

i

Comments(0 comments)

Comments Not Found