Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Query Analyzer - Multiple criteria in Where Clause 2

Status
Not open for further replies.

JcTon

MIS
Oct 26, 2007
16
US
Not obtaining the correct result set using the following in the WHERE clause in a SQL Query Analyzer query;

Where client_id number between '124.5' and '130.5' or between '305' and '340.2' or between '409.1' and 415.25'

In other words, some of the records in the result set shouldn't be there because they contain a field that "violates" another criteria in the WHERE clause.
 
Was there a question hidden there, somewhere in your post?

Try posting the query that you're running and what exactly you want it to do. Then someone will be able to help you.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B> bites again.[/small]
 


Is it possible to specify multiple criteria within the WHERE clause of a sql query that invoves multiple instances of a range of numbers?

For example,

Select Client_Name, Client_ID, Service_Date
Where Service_Date > '05/01/2006' AND '04/15/2007'
and Client_id number between '124.5' and '130.5' or between '305' and '340.2' or between '409.1' and 415.25'

Currently, some of the records in the result set shouldn't be there because they have a Service_Date that is before 5/1/2006 or after 4/15/2007.

 
AND (client_id number between '124.5' and '130.5')
or (client_id number between '305' and '340.2')

does not work.

I still receive records that violate the first criteria.

I believe that the use of "or" is the problem. Therefore, how should the WHERE clause be rewritten so that I extract all records that satisfy the first criteria and have a client_id number between '124.5' and '130.5' or has a client_id number between '305' and '340.2', etc.?
 
You have incrrect syntax in your where clause due to:

1) The date clause should contain the name before the second date
2) The parenthesis are still not correct

Try this example:
Code:
DECLARE @TABLE1 TABLE 
	(id int IDENTITY(1,1), clientid decimal(10,1), servicedate datetime)

INSERT @TABLE1 VALUES (1.5, '20070101')
INSERT @TABLE1 VALUES (1.7, '20070201')
INSERT @TABLE1 VALUES (1.9, '20070301')
INSERT @TABLE1 VALUES (2.1, '20070401')

SELECT id, clientid, servicedate
FROM @TABLE1
WHERE servicedate BETWEEN '20070102' AND '20070402'
AND ((clientid between 1.5 and 1.7) or (clientid between 2.0 and 2.1))


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top