Where conditions ignoring parenthesis during execution.

SQL 2016 Where statement seems to be ignoring parenthesis,  I am trying to capture work transactions for for a day that has three shifts.  The day starts on 02/14 at 6:00 am and ends at the end of third shift on 2/15 at 6:00 am.  The database does not use datetime but rather has it broken into two separate fields.  When I execute it I get no results.  If I take out the time portion of the selection, I get all of the transactions for both days.  Any suggestions?  Here is the statement:

where ((date_work_trx >= '2018-02-14' and time_work_trx > 060000) and (date_work_trx <= '2018-02-15' and time_work_trx <= 060000))
Paul PingelIT ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You fail in the time portion, because one condition only allows for time later than 0600, the other earlier than or equal to 0600. A common mistake.
For the lower boundary, you need to check for greater as the date, or equal to the date and greater than 0600 for the time. And similar for the upper boundary.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
  (date_work_trx > '2018-02-14' or date_work_trx = '2018-02-14' and time_work_trx > 060000)
  (date_work_trx < '2018-02-15' or date_work_trx = '2018-02-15' and time_work_trx <= 060000)

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
When you work only with AND the parenthesis aren't needed at all. If I rewrite your WHERE clause like this you might understand it better:
where ((date_work_trx >= '2018-02-14' and date_work_trx <= '2018-02-15' ) and (time_work_trx > 060000 and time_work_trx <= 060000)

Open in new window

Your issue is that you don't have the time stored with the date field and that's making the filter to be more complex to write. If you can have a field to store both date and time then your query will be more simple:
where date_work_trx > '2018-02-14 06:00:00' and date_work_trx <= '2018-02-15 06:00:00'

Open in new window

Paul PingelIT ManagerAuthor Commented:
Thank you for your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.