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
 Amount between parameters like < or >

Author  Topic 

chennaraaj
Starting Member

17 Posts

Posted - 2014-04-07 : 04:41:17
Hi All,

I am having table Employee with Employee Salaries details,
from that i need to get the values based on parameters(@Param1,@Param2)

select * from Employee
where Salaries >@Param1 and Salaries < @Param2

Conditions :

1) @Param1 >10000 and @Param2 <25000
2) @Param1 >10000 and @Param2 =''
3) @Param1 <15000 and @Param2 =''
(may or may not pass the values for @Param2)

rk

MuralikrishnaVeera
Posting Yak Master

129 Posts

Posted - 2014-04-07 : 05:04:05
Are you looking for this......

DECLARE @param1 INT = 10000
DECLARE @Param2 INT = 25000
DECLARE @Param3 INT = NULL
DECLARE @Param4 INT = 15000

SELECT * FROM Employee WHERE Salary BETWEEN @param1 AND @Param2

SELECT * FROM Employee WHERE Salary > @param1 AND @Param3 IS NULL

SELECT * FROM Employee WHERE Salary < @Param4 AND @Param3 IS NULL

---------------
Murali Krishna

You live only once ..If you do it right once is enough.......
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2014-04-07 : 21:58:07
How can you tell whether to use 2 or 3 if you leave out param2?

I think what you are after is

select * from Employee
where Salaries >@Param1 and (Salaries < @Param2 OR @Param2 is null)
Go to Top of Page

MuralikrishnaVeera
Posting Yak Master

129 Posts

Posted - 2014-04-08 : 00:36:30
quote:
Originally posted by LoztInSpace

How can you tell whether to use 2 or 3 if you leave out param2?

I think what you are after is

select * from Employee
where Salaries >@Param1 and (Salaries < @Param2 OR @Param2 is null)





Sorry to say you missed the third condition Which is not possible to write in a single query........
LoztInSpace
---------------
Murali Krishna

You live only once ..If you do it right once is enough.......
Go to Top of Page
   

- Advertisement -