SQL CONCAT, Concatenate 字符串串连函数

SQL CONCAT函数用于串连(组合)2个或更多个字符串。不同的数据库有不同的写法:

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

SQL CONCAT 函数语法

CONCAT(string1, string2, string3, ...)

注:在Oracle中, CONCAT函数仅能有2个参数,但使用||可以有超过2个参数。

SQL CONCAT 函数范例

数据表: Employees

EmployeeId FirstName LastName
1 Mazojys Fxoj
2 Jozzh Lnanyo
3 Syllauu Dfaafk
4 Gecrrcc Srlkrt
5 Jssme Bdnaa
6 Dnnaao Errllov
7 Tyoysww Osk

选择EmployeeId, 连接FirstName和LastName作为FullName:

例1,SQL Server

SELECT EmployeeId, FirstName + ' ' + LastName AS FullName
FROM Employees

例2,Oracle

SELECT EmployeeId, FirstName || ' ' || LastName AS FullName
FROM Employees

例3,mySQL

SELECT EmployeeId, CONCAT(FirstName, ' ', LastName) AS FullName
FROM Employees

结果会类似于:

EmployeeId FullName
1 Mazojys Fxoj
2 Jozzh Lnanyo
3 Syllauu Dfaafk
4 Gecrrcc Srlkrt
5 Jssme Bdnaa
6 Dnnaao Errllov
7 Tyoysww Osk