Concat function
The string functionCONCAT
has the role of concatenating two or more strings into a single string.
The CONCAT function can be used with NULL values, CONCAT function implicitly converts null values to empty strings.
Basic Concat Example
SELECT CONCAT ('Daniel', ' - ','Scott') AS FullName;
Result: Daniel - Scott
Using CONCAT with NULL values
SELECT CONCAT( 'Test', NULL, 'SQL' );
Result: TestSQL
Complex Concat Example
Cities table:
CITY_ID | NAME | STATE |
---|---|---|
1 | New York | New York |
2 | Los Angeles | California |
3 | Chicago | Illinois |
4 | San Antonio | Texas |
5 | San Diego | California |
SELECT CONCAT (city_id, ' / ', name, ' / ', state) AS Result
FROM cities WHERE state='California';
Concat Result:
Result |
---|
2 / Los Angeles / California |
5 / San Diego / California |
See also: T-SQL Functions -> Charindex -> Left -> Len -> Lower -> Ltrim