SQL IF BETWEEN

Mr_Shaw
Mr_Shaw used Ask the Experts™
on
Is there a way in which I can write a IF BETWEEN statement in SQL. For example;

IF @variable1 BETWEEN 11904 AND 12031
THEN
END
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
If you want to use it inside an SQL statement, you have to use CASE:

CASE
WHEN @variable BETWEEN 11904 AND 12301 THEN x ELSE y END
Top Expert 2012
Commented:
You do it like this:
IF @variable1 BETWEEN 11904 AND 12031
   BEGIN
           --  Your code goes here
   END
yes,e.g.,

DEClARE @variable1 INT
 SET @variable1=12000 
 IF @variable1 BETWEEN 11904 AND 12031
 SELECT  @variable1
 ELSE
 SELECT NULL

Open in new window

11/26 Forrester Webinar: Savings for Enterprise

How can your organization benefit from savings just by replacing your legacy backup solutions with Acronis' #CyberProtection? Join Forrester's Joe Branca and Ryan Davis from Acronis live as they explain how you can too.

Imran Javed ZiaConsultant Software Engineer - .NET Architect
Commented:
Please try it as following

Declare @variable1 as int

set @variable1 = 12000
IF @variable1 BETWEEN 11904 AND 12031
Begin
      print ('Pass')
END
else
Begin
      print ('Fail')
END


Thanks
DEClARE @variable1 INT
 SET @variable1=12000 
 IF @variable1 BETWEEN 11904 AND 12031
BEGIN
 SELECT  @variable1
END
 ELSE
BEGIN
 SELECT NULL
END

Open in new window

Author

Commented:
thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial