Solved

SQL Dates

Posted on 2014-12-05
11
90 Views
Last Modified: 2014-12-15
My results are not returning the correct results.

--[ApptBookedDate] datetime
--YYYY-MM-DD HH:MI:SS

WHERE
        CONVERT(DATETIME, o.ApptBookedDateTime)  < '2014-04-12 14:00:00:000'  

returns - 2014-12-04 13:59:00.000
0
Comment
Question by:aneilg
[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
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40482512
Hi,

not really sure what is the problem here, what should be returned i.e. desired output?
0
 

Author Comment

by:aneilg
ID: 40482519
I want dates returned < 12th April 2014 - 2pm

my results return 04th December 2014.
0
 

Author Comment

by:aneilg
ID: 40482521
o.ApptBookedDateTime_dte is a datetime field.

so I should be able just to do         o.ApptBookedDateTime_dte  < '2014-04-12 14:00:00:000
0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 

Author Comment

by:aneilg
ID: 40482522
this seems to work. o.ApptBookedDateTime_dte  < '2014-04-12'  

if I remove the HH:MI:SS but I need them in the query.
0
 
LVL 50

Accepted Solution

by:
Vitor Montalvão earned 333 total points
ID: 40482530
You can try this:
WHERE 
         CONVERT(DATETIME, o.ApptBookedDateTime)  < CONVERT(VARCHAR,'2014-04-12 14:00:00:000',114)

Open in new window

0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 167 total points
ID: 40482537
CONVERT(DATETIME, o.ApptBookedDateTime)

your field IS already datetime, so you shall not apply the convert function there:
see this article:
http://www.experts-exchange.com/Database/Miscellaneous/A_1499-DATE-and-TIME-don%27t-be-scared-and-do-it-right-the-first-time.html
0
 

Author Comment

by:aneilg
ID: 40482544
still returns 2014-12-04 13:59:00.000

04th December
0
 
LVL 50

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 333 total points
ID: 40482553
Then try with code 120:
WHERE 
         CONVERT(DATETIME, o.ApptBookedDateTime)  < CONVERT(VARCHAR,'2014-04-12 14:00:00:000',120)

Open in new window

In this MSDN article you can see all available codes.
0
 

Author Comment

by:aneilg
ID: 40482556
thanks.

forgot to add

SET DATEFORMAT MDY
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40482574
>still returns 2014-12-04 13:59:00.000
so, you want to format the OUTPUT ...

which is in the SELECT itself:

SELECT CONVERT(varchar(20), o.ApptBookedDateTime, 101)
 FROM ...
WHERE  o.ApptBookedDateTime  < CONVERT(datetime, '2014-04-12 14:00:00:000'  , 120)
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40482945
<wild guess>
Copy-paste the COMPLETE WHERE clause into this question, just in case there is any logic such as AND / OR or any precedence constraints ( ) that is causing this date comparison to only fire on a subset of rows.
0

Featured Post

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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