Solved

SQL syntax

Posted on 2011-09-20
6
220 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
Comment Utility
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
Comment Utility
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]
Comment Utility
no, but it won't do any bad.
it's the () around the OR ...
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:deanlee17
Comment Utility
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]
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.​
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

11 Experts available now in Live!

Get 1:1 Help Now