The Data Management Center

SQL Concatenation


David Haertzen David Haertzen, Tutorial Author
Check out our
Data Warehousing Tutorial.

HOME | SQL OVERVIEW | SQL BASICS | SQL ADMIN | SQL ADVANCED | SQL SYNTAX | Bookmark and Share
Previous | Next

SQL > SQL Advanced > SQL Concat / Concatenation


What is SQL Concatenation?

Concatenation is the process of combining data strings.

For example, the concatenation of 'John', ' ', 'W', '. ' and 'Smith' is

'John W. Smith'

Also, the concatenation of '212', '-', '555', '-' and '1234' is

'212-555-1234'

Why Use SQL Concatenation?

Concatenation can result in more readable output while maintaining data in separate columns for greater flexibility.

How To Use SQL Concatenation

The means of achieving concatenation differs by database type. for example:

  • MySQL uses the CONCAT() function
  • Oracle uses the CONCAT() function and the || operator
  • SQL Server uses the + operator

More than 2 strings can be combined through concatenation. Numbers must be converted to strings before concatenation.

SQL Concatenation Syntax

 
<value_1> + <value_2>
 

SQL Concatenation Example

The following example concatenates the column branch_name with column region_name along with spaces and parens for readability.

Here are the contents of the tables:

Table: REGION
region_nbrregion_name
100East Region
200Central Region
300Virtual Region
400West Region

Table: BRANCH
branch_nbrbranch_nameregion_nbremployee_count
108New York10010
110Boston1006
212Chicago2005
404San Diego4006
415San Jose4003

This SQL Statement with concatenation is executed:

 
SELECT branch.branch_name + ' (' + region.region_name + ')'
FROM dbo.region
JOIN dbo.branch
ON branch.region_nbr = region.region_nbr
ORDER BY region.region_nbr

Here is the result.

branch_name (region_name)
New York (East Region)
Boston (East Region)
Chicago (Central Region)
San Diego (West Region)
San Jose (West Region)

HOME | SQL OVERVIEW | SQL BASICS | SQL ADMINISTRATION | SQL ADVANCED | SQL SYNTAX
infogoal.com HOME
SQL OVERVIEW
SQL SYNTAX
SQL BOOKREVIEWS
SQL BASICS
SQL SELECT
SQL WHERE
SQL INSERT
SQL UPDATE
SQL DELETE
SQL ADMINISTRATION
SQL CREATE DATABASE
SQL DROP DATABASE
SQL CREATE TABLE
SQL ALTER TABLE
SQL DROP TABLE
SQL CREATE INDEX
SQL DROP INDEX
SQL ADD FOREIGN KEY
SQL DROP FOREIGN KEY
SQL CREATE VIEW
SQL DROP VIEW
SQL ADVANCED
SQL CONCAT
SQL SUBSTRING
SQL TRIM
SQL AND & OR
SQL IN
SQL BETWEEN
SQL LIKE
SQL DISTINCT
SQL GROUP BY
SQL AGGREGATE
SQL HAVING
SQL ORDER BY
SQL JOIN
SQL OUTER JOIN

Copyright© 1999-2014, First Place Software, Inc.