Solved

Days between two dates

Posted on 2010-11-26
6
409 Views
Last Modified: 2012-05-10
I'm trying to compare two days that I pull out of a mysql query and get the difference between them

i.e. the number of days between 2008-01-27 and 2009-04-25.

Is there a simple way of doing this?
0
Comment
Question by:PeterErhard
  • 3
  • 3
6 Comments
 
LVL 37

Expert Comment

by:Harisha M G
ID: 34220721
0
 

Author Comment

by:PeterErhard
ID: 34220726
Thanks for that.

How come this doesn't work?
Error
SQL query: 

SELECT DateCreated, DATE( ) - TO_DAYS( DateCreated ) 
FROM TABLE
LIMIT 0 , 30

MySQL said: 

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') - TO_DAYS( DateCreated )
FROM AskTheSpider
LIMIT 0, 30' at line 1

Open in new window

0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 34220731
Change DATE( ) to TO_DAYS(DATE( ))
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:PeterErhard
ID: 34220735
Getting this now. Any Ideas?
SQL query: 

SELECT DateCreated, TO_DAYS( DATE( ) ) - TO_DAYS( DateCreated ) 
FROM AskTheSpider
LIMIT 0 , 30

MySQL said: 

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')) - TO_DAYS(DateCreated) 
FROM AskTheSpider
LIMIT 0 , 30' at line 1

Open in new window

0
 
LVL 37

Accepted Solution

by:
Harisha M G earned 500 total points
ID: 34220752
Sorry.. it should be CURDATE()

SELECT DateCreated, TO_DAYS(CURDATE()) - TO_DAYS( DateCreated )
FROM TABLE
LIMIT 0 , 30;

0
 

Author Comment

by:PeterErhard
ID: 34220756
Thanks very much, that did the trick :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

840 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