Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 help me lessen the code please

Author  Topic 

payb36
Starting Member

1 Post

Posted - 2012-08-29 : 22:44:09
is there any way to lessen my code in ms sql? btw, this is my code

select
[Index] = (case
when
amount >= 1 and amount <= 50
then '1'
when
amount >= 51 and amount <= 100
then '2'
when
amount >= 101 and amount <= 151
then '3'
end),

[Range] = (case
when
amount >= 1 and amount <= 50
then '1-51'
when
amount >= 51 and amount <= 100
then '50-101'
when
amount >= 101 and amount <= 151
then '100-151'
end)
from tbl_1


the case seems to be the same so i'm wondering if you guys can lessen up this code?(btw i need those two columns)

think of another way around

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-08-29 : 23:00:03
You can make it slightly shorter:
select
[Index] = (case when amount <= 50 then '1'
when amount <= 100 then '2'
when amount <= 151 then '3' end),
[Range] = (case when amount <= 50 then '1-51'
when amount <= 100 then '50-101'
when amount <= 151 then '100-151' end)
from tbl_1
You don't need to do the >= check on each WHEN condition, it's implied by the preceding WHEN.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-30 : 01:23:47
why not add a range table with start and end values and just do a join to that to retrieve the range description?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -