Solved

check range with sql

Posted on 2014-01-22
3
226 Views
Last Modified: 2014-01-23
I have a variable in my sql server store proc and i want to check if the variable is in between certain numbers

so if  @variable is in between 100-199 or @variable in between 600-699 Or variable in between 900-999

begin
 -- do my query of over here
end

How do i check if my variable falls in that range that i have mentioned above
0
Comment
Question by:pratikshahse
3 Comments
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 39801344
declare @test int


select @test = case when @var between 100 and 199 then 1
                                 when @var between 600 and 699 then 1
                                 when @var between 900 and 999 then 1
                                 else 0 end

if @test = 1
begin
  -- query
end
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39801345
ok you can add the between clause to the where clause of the query

select * 
from <your Table>
where <your cond>
AND @VAR BETWEEN <Start> and <end>

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39801352
The below is easier to read...
SELECT blah, blah, blah
FROM YourTable
WHERE 
   @variable between 100 AND 199 OR
   @variable between 600 AND 699 OR
   @variable between 900 AND 999

Open in new window

... although Kyle's code is preferred, as if the @variable does not fall in these ranges then the query will not be performed, saving that unneeded execution time.
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now