Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to compare DateTime values?

Posted on 2014-10-13
7
Medium Priority
?
308 Views
Last Modified: 2014-10-13
Experts,

I am trying write a MySQL query to compare a static datetime value vs. the datetime values stored in my database. I am having trouble doing so.

The datetime values ("JobStartDate") in my database have the following format: '1899-12-30 xx:xx:xx'

The example query below should demonstrate what I am trying to do:

$query = "SELECT * FROM table WHERE JobStartDate < '1899-12-30 09:59:00'";

Can someone please assist me in correcting my syntax so that the two datetime values are evaluated correctly?
0
Comment
Question by:evibesmusic
  • 3
  • 2
  • 2
7 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 1000 total points
ID: 40377647
The basics of your query look OK. I cannot see an obvious syntactic error. Are you getting an error message that leads you to believe your syntax is wrong?
0
 

Author Comment

by:evibesmusic
ID: 40377654
@Beverley Portlock:

I am not getting an error but, I am not getting any results from the query when I know results should exist.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 40377656
Can you post some sample data and the fragment of PHP/MySQL code that is not working as you expect?
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 total points
ID: 40377720
This article shows some of the ways of dealing with DATETIME values in PHP and MYSQL.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Key points: All date / time values for internal use should be held in ISO-8601 format.  In the MySQL column definition, this is data type DATETIME.  In PHP this is date('c') or date('Y-m-d H:i:s').  The format is both easy to read and well-standardized allowing comparison and sorting.

If you're using the BETWEEN clause in a query, you may want to be aware of this:
http://www.experts-exchange.com/Programming/Languages/SQL_Syntax/A_11210-Beware-of-Between.html
0
 

Author Comment

by:evibesmusic
ID: 40378516
@all:

The column in my database was defined as a "text" data type and not a "datetime" data type. I've updated the data type and now my query works.

Cheers!
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 40378537
Thank you. I am glad you got it sorted.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40378654
Bingo!  Glad it's pointed in the right direction.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

877 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