Solved

check range with sql

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 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