查询语句时利用单行函数并连接

问题描述 投票:0回答:1

尝试连接一条语句尝试得到以下结果;

josh is their name and they earn $x.

我写的声明有误;

Select 
    concatenate (lower (ename), 'is their name', 'and they earn' (sal)) as chart 
from emp: 
sql sql-server concatenation
1个回答
0
投票

虽然 SQL Server 中有 concat() 函数,但您可以简单地使用 + 运算符来连接字符串。您还可以使用 Format() 来格式化值。即:

create table emp (ename varchar(20), sal decimal(20,4));
insert into emp (ename, sal) values 
  ('Josh', 1000),
  ('Mike', 1200),
  ('Ian', 1500);



Select lower (ename) + 
   ' is their name and they earn ' + 
    FORMAT(sal, '$#,#.##') as chart 
from emp; 
图表
josh 是他们的名字,他们的收入为 1,000 美元
mike 是他们的名字,他们的收入为 1,200 美元
ian 是他们的名字,他们的收入为 1,500 美元

DBfiddle 演示

© www.soinside.com 2019 - 2024. All rights reserved.