简体   繁体   中英

Case expression on a created column

I have set up a view in Microsoft SQL Server. The portion of the view which I am showing creates a column named Starch. How can I manipulate that created column within a case expression to decide if it is above or below 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',
...
..
.

Assuming your ISNUMERIC still doesn't cause a conversion error (which it could depending on the data, even when it = 1) then you could wrap this in a CTE among other things.

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

Example of ISNUMERIC() Failing... Sort of... Atleast for your case

declare @table table (i varchar(16))
insert into @table
values
('$'),
('1e4'),
('1,256,233'),
('5D105')

select isnumeric(i) from @table

These all return true, but would fail the convert...

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