

Each argument of the expression needs to be of the VARCHAR type if the concatenation will be successful. With the + operator, none of the arguments will be converted automatically. Here is an example of using the + operator to concatenate a user’s first and last names:įirst_name + ' ' + last_name AS full_name It takes two or more arguments and returns a single concatenated string. The + operator is used to concatenate strings in MS SQL Server. This allows us to avoid concatenating the NULL value of Robocop’s last name and getting a NULL result. If the last name is NULL, it returns the empty string ‘’. We want to avoid a bad concatenation, so we use the COALESCE() function:įirst_name || ' ' || COALESCE(last_name, '') AS full_nameĪbove, COALESCE() returns the last name if the last name is not NULL. In the following example, suppose that the last_name field could be NULL. The function takes one or more arguments and returns the first argument that is not NULL.

The COALESCE() function in SQL is a built-in function that returns the first non-NULL value in a list of expressions. Use the COALESCE() Function to Concatenate NULL ValuesĪn incredible tip for avoiding NULL values is using the COALESCE() function. The concatenation “ignores” the NULL string, and the concatenated arguments are returned.

Note: In the case of Oracle, a NULL string is an empty string. If you concatenate ' Robocop' with NULL, you get NULL. The second result here is NULL: Robocop has a first name, but his middle and last names are NULL.

In the following example, we’ll try a concatenation with a NULL argument:įirst_name || ' ' || last_name AS full_name If one of the arguments of the concatenation is NULL, the whole expression returns NULL. Imagine that you have some empty or NULL values in your database: The | | concatenation operator returns NULL for any NULL argument.
Mysql concat how to#
Here is an example of how to use the || operator to concatenate the ID and the first name of a user to form a unique identifier:Ĭast(id as VARCHAR) || '_' || first_name AS unique_id If not, you need to cast it into a text type. Note that the first argument of the concatenation must be of a text data type. Here is an example of how to use the || operator to concatenate the first name, the last name, and the age of a user to form a new string:įirst_name || ' ' || last_name || ': ' || age || ' yo' AS user_details They will be automatically converted to a string. The two previous examples only contain strings what about the other data types? The arguments of the || operator can be strings, text columns, expressions, and other data types like numbers or dates. The Concatenation Operator || Works with Non-Text Columns Too SELECT first_name || ' ' || middle_name || ' ' || last_name AS full_name For example, the following SQL statement concatenates users’ first, middle, and last names: You can concatenate multiple strings using the || operator by providing more than two arguments. The result is then aliased as full_name and returned in the query results.
Mysql concat full#
In this example, the || operator takes the first_name and last_name columns from the userstable and concatenates them together with a space in between, resulting in a full name for each user. SELECT first_name || ' ' || last_name AS full_name Let’s use the || operator to concatenate users’ first and last names and get a full name: Id first_name middle_name last_name age marital_status We have a table called users that stores user information: The || operator takes two or more arguments and returns a single concatenated string. Most SQL databases, with the notable exception of SQL Server, support this operator. Standard SQL uses the || operator (as well as a few other options). Here are a few examples of the most well-known techniques for SQL concatenation. The syntax for SQL concatenation can vary depending on the specific SQL dialect being used. With each exercise solved, you build confidence in your skills. Taking the course is a great way to refresh your SQL knowledge. It contains almost 90 hands-on exercises, which are divided into five topic-based sections. The best way to practice SQL, including SQL concatenation, is our interactive SQL Practice Set. These are just a few examples, but SQL concatenation can be used in many other situations where combining multiple strings is necessary.
