我在Microsoft SQL Server中设置了一个视图。我正在显示的视图部分创建了一个名为Starch的列。如何在case表达式中操作创建的列以确定它是高于还是低于2?
.
..
...
MAX(CASE WHEN R.ANALYTE = 'Starch' then
(CASE WHEN ISNUMERIC(R.RN5) = 1 THEN CONVERT(float,R.RN5) ELSE convert(float,0) END) end) as [Starch],
CASE
WHEN Starch > 2 THEN 'ABOVE'
ELSE 'Below' END
As 'Starch_Cautionary',
...
..
.
答案 0 :(得分:1)
假设您的ISNUMERIC
仍然没有导致转换错误(可能取决于数据,即使它= 1),那么您可以将其包装在CTE
等其他内容中
with cte as(
.
..
...
MAX(CASE WHEN R.ANALYTE = 'Starch' then
(CASE WHEN ISNUMERIC(R.RN5) = 1 THEN CONVERT(float,R.RN5) ELSE convert(float,0) END) end) as [Starch],
...
..
.)
select
*,
CASE
WHEN Starch > 2 THEN 'ABOVE'
ELSE 'Below' END
As 'Starch_Cautionary',
...
..
.
from cte
ISNUMERIC()失败的例子......根据你的案例判断
declare @table table (i varchar(16))
insert into @table
values
('$'),
('1e4'),
('1,256,233'),
('5D105')
select isnumeric(i) from @table
这些都返回true,但转换失败......