Solved

SQL syntax

Posted on 2011-09-20
6
231 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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36567463
no, but it won't do any bad.
it's the () around the OR ...
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 142

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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.​
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now