Solved

SQL Dates

Posted on 2014-12-05
11
87 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
  • 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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 47

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 142

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 47

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 142

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 65

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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

Suggested Solutions

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…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

16 Experts available now in Live!

Get 1:1 Help Now