[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL syntax

Posted on 2011-09-20
6
Medium Priority
?
267 Views
Last Modified: 2012-05-12
Hi guys, I have an error in my SQL somewhere. When I run...

"SELECT MESDATE,LOCATION,ALLOCATION,OWNERSHIPTYPE,PARTNBR,VERSION,PARTDESCRIPTION,QTY,TRANSTYPE,REASONCODE,REASONDESC,TRANSCOMMENT" & _
                    ",GRNNBR,SERIALNBR,CURRENCY,UNITCOST,TOTALBASELINECOST,COMPARTNBR FROM tblMestecData WHERE " & _
                    " MESDATE between '2011-07-17' AND '2011-08-13'

I get the correct data with the date filter. When I run

"SELECT MESDATE,LOCATION,ALLOCATION,OWNERSHIPTYPE,PARTNBR,VERSION,PARTDESCRIPTION,QTY,TRANSTYPE,REASONCODE,REASONDESC,TRANSCOMMENT" & _
                    ",GRNNBR,SERIALNBR,CURRENCY,UNITCOST,TOTALBASELINECOST,COMPARTNBR FROM tblMestecData WHERE " & _
                    " MESDATE between '2011-07-17' AND '2011-08-13' AND TRANSTYPE= 'Use' OR TRANSTYPE= 'Stock Adj' Order By MESDATE"

The date range is ignored?

Thanks,
Dean
0
Comment
Question by:deanlee17
  • 3
  • 3
6 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 36567433
because of the OR.

please use () around AND and ORed conditions
"SELECT MESDATE,LOCATION,ALLOCATION,OWNERSHIPTYPE,PARTNBR,VERSION,PARTDESCRIPTION,QTY,TRANSTYPE,REASONCODE,REASONDESC,TRANSCOMMENT" & _
                    ",GRNNBR,SERIALNBR,CURRENCY,UNITCOST,TOTALBASELINECOST,COMPARTNBR FROM tblMestecData WHERE " & _
                    " MESDATE between '2011-07-17' AND '2011-08-13' AND ( TRANSTYPE= 'Use' OR TRANSTYPE= 'Stock Adj' ) Order By MESDATE"

Open in new window

0
 

Author Comment

by:deanlee17
ID: 36567446
So id need the AND around the 2 dates too?..

(MESDATE between '2011-07-17' AND '2011-08-13' )?

Thanks
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36567463
no, but it won't do any bad.
it's the () around the OR ...
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:deanlee17
ID: 36567475
Ok a little confused why i would put it around

( TRANSTYPE= 'Use' OR TRANSTYPE= 'Stock Adj' )

and not

MESDATE between '2011-07-17' AND '2011-08-13'

?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36567494
let's explain it a bit with how sql will evaluate:

WHERE <condition1>  AND  TRANSTYPE= 'Use' OR TRANSTYPE= 'Stock Adj'
will execute like this:
WHERE ( <condition1>  AND  TRANSTYPE= 'Use' ) OR TRANSTYPE= 'Stock Adj'

so, it will return all the rows for TRANSTYPE = 'Stock Adj' for which the <condition 1> is ignored.


MESDATE between '2011-07-17' AND '2011-08-13'
is evaluated the same as
(MESDATE between '2011-07-17' AND '2011-08-13' )
resp:
MESDATE >= '2011-07-17' AND MESDATE <= '2011-08-13'
aka
(MESDATE >= '2011-07-17' AND MESDATE <= '2011-08-13')

hope this helps
0
 

Author Comment

by:deanlee17
ID: 36567522
That will do nicely, thanks for taking the time to explain that.

I now have to change those fixed dates for combo boxes on a form, but I guess thats for another post.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Screencast - Getting to Know the Pipeline
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

834 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