Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL syntax

Posted on 2011-09-20
6
Medium Priority
?
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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.

 

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

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

636 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