如何在 SQL Server 中连接字符串消息和列值

Dis*_*ant 2 sql sql-server

我正在尝试为我的 sql 查询结果添加自定义消息,但找不到确切的方法来实现这一点。下面是我在 sql 查询中尝试的结果。

Select 
    'The Maximum Rating of the city'  " + city + "  'is=' " + MAX(rating) + "
From 
    CUSTOMER 
Where 
    CITY is not null 
Group by 
    CITY;
Run Code Online (Sandbox Code Playgroud)

Pரத*_*ீப் 5

删除双引号和最后一个+运算符

Select 'The Maximum Rating of the city '+city+' is = '+ cast(MAX(rating) as varchar(50))
from CUSTOMER 
where CITY is not null 
group by CITY;
Run Code Online (Sandbox Code Playgroud)

如果您正在使用,Sql Server 2012+那么您可以使用Concat不需要显式转换的函数

Select Concat('The Maximum Rating of the city ',city,' is = ', MAX(rating))
from CUSTOMER 
where CITY is not null 
group by CITY;
Run Code Online (Sandbox Code Playgroud)