Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 456
  • Last Modified:

Case Else (SQL Server 2000)

Is it possible to make this query shorter

select v.temper,v.finish_id,v.grade,weight,
       case when v.thick between 0.17 and 0.209 then '1720' else
       case when v.thick between 0.21 and 0.229 then '2122' else
       case when v.thick between 0.23 and 0.279 then '2327' else
       case when v.thick between 0.28 and 0.359 then '2835' else
       case when v.thick between 0.36 and 0.399 then '3640' else
       case when v.thick between 0.40 and 99999 then '4099' end end end end end end
from v_mkt_stock_etlcoil v




0
Mateen
Asked:
Mateen
1 Solution
 
KarinLoosCommented:

select v.temper,v.finish_id,v.grade,weight,
       case when v.thick between 0.17 and 0.209 then '1720'
              when v.thick between 0.21 and 0.229 then '2122'
              when v.thick between 0.23 and 0.279 then '2327'
              when v.thick between 0.28 and 0.359 then '2835'
              when v.thick between 0.36 and 0.399 then '3640'
              when v.thick between 0.40 and 99999 then '4099' end
from v_mkt_stock_etlcoil v
0
 
hkamalCommented:
First of, you don't need to repeat the CASE; viz:
select v.temper,v.finish_id,v.grade,weight, case
       when v.thick between 0.17 and 0.209 then '1720'
       when v.thick between 0.21 and 0.229 then '2122'
       when v.thick between 0.23 and 0.279 then '2327'
       when v.thick between 0.28 and 0.359 then '2835'
       when v.thick between 0.36 and 0.399 then '3640'
       when v.thick between 0.40 and 99999 then '4099' end
from v_mkt_stock_etlcoil v

Only other way of doing this [especially if you use these values again] is to store them in a temp table  and join it to v_mkt_stock_etlcoil

select v.temper,v.finish_id,v.grade,weight, l.value
from v_mkt_stock_etlcoil v, limits l
where v.thick between l.low_limit and l.high_limit

HTH
0
 
MateenAuthor Commented:
Hi Karinloos:

select v.temper,v.finish_id,v.grade,weight,
       case v.thick
       when 0.21  then '2122'
       when 0.23  then '2327'
       when 0.28  then '2835'
       when 0.36  then '3640'
       when 0.40  then '4099' end
from   v_mkt_stock_etlcoil v
here column(v.thick) is referred one time. Is it not possible in my question query?
0
 
KarinLoosCommented:
Hi Mateen the above is valid, but in your original question you were querying v.thick to a range.

0
 
OtanaCommented:
If there are no other possibilities then the ones you specified, you could change the last line:

select v.temper,v.finish_id,v.grade,weight,
       case when v.thick between 0.17 and 0.209 then '1720'
              when v.thick between 0.21 and 0.229 then '2122'
              when v.thick between 0.23 and 0.279 then '2327'
              when v.thick between 0.28 and 0.359 then '2835'
              when v.thick between 0.36 and 0.399 then '3640'
              else '4099' end
from v_mkt_stock_etlcoil v
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now