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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to do this using the between?

Author  Topic 

CVDpr
Starting Member

41 Posts

Posted - 2009-06-09 : 11:16:00
select CompanyId, FacilityId, InsId from myTbale
where CompanyId = @CompanyId and FacilityId = @FacilityId
and (InsId = @insId or @insId = -1)



How i do the (InsId = @insId or @insId = -1)but using between, Thanks.

asgast
Posting Yak Master

149 Posts

Posted - 2009-06-09 : 12:09:01
you don't

currently in the code you check insid for 2 possible values @insID and -1

using between you would be checking for a range of values, query would return different results

try WHERE InsId BETWEEN -1 AND @insID if you want to check the range
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-09 : 12:17:24
quote:
Originally posted by asgast

you don't

currently in the code you check insid for 2 possible values @insID and -1

using between you would be checking for a range of values, query would return different results

try WHERE InsId BETWEEN -1 AND @insID if you want to check the range


but this wont be equivalent to code that OP posted
Go to Top of Page

CVDpr
Starting Member

41 Posts

Posted - 2009-06-09 : 13:57:42
thats the problem, i want if the variable @InsID1=-1, and @InsId2=-1 = show all the records else
do the between @InsId1 and @InsId2.:-(
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-09 : 14:00:45
aha..where does this @InsID2 come from? in that case it should be


select CompanyId, FacilityId, InsId from myTbale
where CompanyId = @CompanyId and FacilityId = @FacilityId
and ((@InsID1=-1 and @InsId2=-1) or (InsId BETWEEN @InsID1 AND @InsID2))
Go to Top of Page

CVDpr
Starting Member

41 Posts

Posted - 2009-06-09 : 14:22:09
Yes, that work thanks.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-09 : 14:27:40
welcome
Go to Top of Page
   

- Advertisement -