Solved

check range with sql

Posted on 2014-01-22
3
233 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 40

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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