Solved

SQL Dates

Posted on 2014-12-05
11
89 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 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