SQL > SQL String Functions > Concatenate

Sometimes it is necessary to combine together (concatenate) the results from several different fields. Each database provides a way to do this:

  • MySQL: CONCAT( )
  • Oracle: CONCAT( ), ||
  • SQL Server: +

The syntax for CONCAT( ) is as follows:

CONCAT (str1, str2, str3, ...)

The above syntax concatenates str1, str2, str3, and any other strings together. Please note the Oracle CONCAT( ) function only allows two arguments -- only two strings can be put together at a time using this function. However, it is possible to concatenate more than two strings at a time in Oracle using '||'.

Let's look at some examples. Assume we have the following table:

Table Geography

Region_NameStore_Name
EastBoston
EastNew York
WestLos Angeles
WestSan Diego

Example 1

MySQL/Oracle:

SELECT CONCAT(Region_Name, Store_Name) FROM Geography
WHERE Store_Name = 'Boston';

Result:

'EastBoston'

Example 2

Oracle:

SELECT Region_Name || ' ' || Store_Name FROM Geography
WHERE Store_Name = 'Boston';

Result:

'East Boston'

Example 3

SQL Server:

SELECT Region_Name + ' ' + Store_Name FROM Geography
WHERE Store_Name = 'Boston';

Result:

'East Boston'

Next: SQL SUBSTRING




Copyright © 2014   1keydata.com   All Rights Reserved.     Privacy Policy