简体   繁体   中英

How to concatenate string message with column value in SQL Server

I am trying to add custom message for my sql query result but can't find exactly how can I achieve that. below is what I tried with my sql query.

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

Remove the Double quotes and last + operator

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;

If you are using Sql Server 2012+ then you can use Concat function which does not require explicit conversion

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM