如何在SQL Server中将字符串消息与列值连接起来

时间:2015-12-12 07:18:41

标签: 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;

1 个答案:

答案 0 :(得分:2)

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

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;

如果您使用的是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;