在 SQL Server 中,可以使用加号运算符(+)来拼接字符串。但是,如果需要拼接多个字符串或表中的字段,就需要使用内置的拼接函数了

以下是 SQL Server 中的一些内置拼接函数:

1. CONCAT:将两个或多个字符串拼接在一起。语法为:
CONCAT (string1, string2, ...)

示例:

SELECT CONCAT('Hello', ' ', 'World') as combined_string;

输出结果为:Hello World
2. CONCAT_WS:与 CONCAT 类似,但可以指定一个分隔符。语法为:
CONCAT_WS (separator, string1, string2, ...)

示例:

SELECT CONCAT_WS('-', 'First', 'Second', 'Third') as combined_string;

输出结果为:First-Second-Third
3. CONCATN:将多个字符串拼接在一起,并在每个字符串之间添加指定的字符。语法为:
CONCATN (separator, string1, string2, ...)

示例:

SELECT CONCATN('-', 'First', 'Second', 'Third') as combined_string;

输出结果为:First-Second-Third
4. REPLACE:用于替换字符串中的指定子字符串。语法为:
REPLACE (string, old_substring, new_substring)

示例:

SELECT REPLACE('Hello World', 'World', 'SQL Server') as replaced_string;

输出结果为:Hello SQL Server
5. +:可以将任何类型的数据转换为字符串并拼接在一起。语法为:
+ expression

示例:

SELECT 'Hello' + CAST(123 AS VARCHAR(10)) as combined_string;
    
输出结果为:Hello123

猜你喜欢

转载自blog.csdn.net/qq_49641620/article/details/133177478