Solved

SQL Dates

Posted on 2014-12-05
11
84 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
 

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 45

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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 45

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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