有条件地将十进制值转换为整数
Conditionally convert a decimal value to integer
create table #Temp
(
id int,
Volume decimal(18,3)
)
insert into #Temp(id,Volume)values(1,10)
insert into #Temp(id,Volume)values(2,10.2)
id Volume
-----------------
1 10.000
2 10.200
Declare @Type as int
set @Type=1
select Id,Convert(varchar(10),CASE WHEN @Type=1 THEN CAST(Volume AS INT)
ELSE Volume END) AS Quantity from #Temp
显示的是这样的结果
id Volume
-----------------
1 10.000
2 10.000
但我想要这样的结果,当类型为 1 时,结果应为整数格式:
id Volume
-----------------
1 10
2 10
在其他条件下(@Type 而不是 1)我想要这样的结果
id Volume
-----------------
1 10.000
2 10.200
我试过这个查询
select Id,Convert(varchar(10),CASE WHEN @Type=1 THEN CAST(Volume AS INT)
ELSE Volume END) AS Quantity from #Temp
这对你有用。
select Id, (CASE WHEN @Type=1 THEN convert(varchar(10),convert(int,volume))
ELSE convert(varchar(10),Volume) END) AS Quantity from #Temp
当@type 为 1 时,两个值的结果均为 10。否则它将是 table.
中的精确值
create table #Temp
(
id int,
Volume decimal(18,3)
)
insert into #Temp(id,Volume)values(1,10)
insert into #Temp(id,Volume)values(2,10.2)
id Volume
-----------------
1 10.000
2 10.200
Declare @Type as int
set @Type=1
select Id,Convert(varchar(10),CASE WHEN @Type=1 THEN CAST(Volume AS INT)
ELSE Volume END) AS Quantity from #Temp
显示的是这样的结果
id Volume
-----------------
1 10.000
2 10.000
但我想要这样的结果,当类型为 1 时,结果应为整数格式:
id Volume
-----------------
1 10
2 10
在其他条件下(@Type 而不是 1)我想要这样的结果
id Volume
-----------------
1 10.000
2 10.200
我试过这个查询
select Id,Convert(varchar(10),CASE WHEN @Type=1 THEN CAST(Volume AS INT)
ELSE Volume END) AS Quantity from #Temp
这对你有用。
select Id, (CASE WHEN @Type=1 THEN convert(varchar(10),convert(int,volume))
ELSE convert(varchar(10),Volume) END) AS Quantity from #Temp
当@type 为 1 时,两个值的结果均为 10。否则它将是 table.
中的精确值