Connecting Tech Pros Worldwide Help | Site Map

SQL Date Calculations

Member
 
Join Date: Mar 2008
Posts: 90
#1: Mar 18 '08
I have a table with with Last_Modified field as VARCHAR(55) which stores dates in the format of 2/15/08 1:34 PM. What I need to do is query the database and select ALL rows that have the Last_Modified field >= 6 days from the current date/time. It appears I will need to convert the dates so I can use the mysql calculations but I'm unsure where to begin on this.
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#2: Mar 18 '08

re: SQL Date Calculations


Quote:

Originally Posted by arggg

I have a table with with Last_Modified field as VARCHAR(55) which stores dates in the format of 2/15/08 1:34 PM. What I need to do is query the database and select ALL rows that have the Last_Modified field >= 6 days from the current date/time. It appears I will need to convert the dates so I can use the mysql calculations but I'm unsure where to begin on this.

You first have to rework your date from the VARCHAR field into a 'normal' MySQL date format like
Expand|Select|Wrap|Line Numbers
  1. .... STR_TO_DATE(field_name, '%m/%d/%y') ....;
and then use that result to compare with the MySQL CURRDATE() values, like
Expand|Select|Wrap|Line Numbers
  1. SELECT * from table_name WHERE DATEDIFF(STR_TO_DATE(field_name, '%m/%d/%'), CURRDATE()) >= 6;
Ronald
Member
 
Join Date: Mar 2008
Posts: 90
#3: Mar 18 '08

re: SQL Date Calculations


Quote:

Originally Posted by ronverdonk

You first have to rework your date from the VARCHAR field into a 'normal' MySQL date format like

Expand|Select|Wrap|Line Numbers
  1. .... STR_TO_DATE(field_name, '%m/%d/%y') ....;
and then use that result to compare with the MySQL CURRDATE() values, like
Expand|Select|Wrap|Line Numbers
  1. SELECT * from table_name WHERE DATEDIFF(STR_TO_DATE(field_name, '%m/%d/%'), CURRDATE()) >= 6;
Ronald

Is there a way to add the time in there as well so for example once that exact hour/minute hit that can be counted as 6 days? Also if this is not possible will str_to_date take out the time for me?
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#4: Mar 18 '08

re: SQL Date Calculations


Yes STR_TO_DATE also can handle the time part. STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or a DATE or TIME value if the string contains only date or time parts.

Better look at the MySQL documentation on STR_TO_DATE or the Date and Time function chapter in same manual HERE

Ronald
Member
 
Join Date: Mar 2008
Posts: 90
#5: Mar 19 '08

re: SQL Date Calculations


Quote:

Originally Posted by ronverdonk

Yes STR_TO_DATE also can handle the time part. STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or a DATE or TIME value if the string contains only date or time parts.

Better look at the MySQL documentation on STR_TO_DATE or the Date and Time function chapter in same manual HERE

Ronald

I can't get the query to return nothing when infact there is data that should match this.

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM opencases_daily INNER JOIN tsms WHERE DATEDIFF(STR_TO_DATE(Last_Out_Note, '%Y-%c-%e'), CURDATE()) >= 6 AND Assigned_To=tsms.Name AND tsms.uid='12' ORDER BY CAST(Last_Out_Note AS UNSIGNED) DESC
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#6: Mar 19 '08

re: SQL Date Calculations


In your start post you said that your data format was varchar mm/dd/yy. So that is the string that you must convert to a normal MySQL date with
Expand|Select|Wrap|Line Numbers
  1. STR_TO_DATE(field_name, '%m/%d/%y') ....;
So why are you now using
Expand|Select|Wrap|Line Numbers
  1. str_to_date(field_name, '%Y-%c-%e')
Ronald
Member
 
Join Date: Mar 2008
Posts: 90
#7: Mar 19 '08

re: SQL Date Calculations


Quote:

Originally Posted by ronverdonk

In your start post you said that your data format was varchar mm/dd/yy. So that is the string that you must convert to a normal MySQL date with

Expand|Select|Wrap|Line Numbers
  1. STR_TO_DATE(field_name, '%m/%d/%y') ....;
So why are you now using
Expand|Select|Wrap|Line Numbers
  1. str_to_date(field_name, '%Y-%c-%e')
Ronald

I made a mistake it should be
Expand|Select|Wrap|Line Numbers
  1. STR_TO_DATE(field_name, '%m/%d/%y')
however it doesnt seem to be returning anything. When I just run DATEDIFF inside of phpMyAdmin SQL it gives me a syntax error

Expand|Select|Wrap|Line Numbers
  1. Error
  2.  
  3.               DATEDIFF( STR_TO_DATE(  '2/15/08',  '%m/%d/%y'  ) , CURDATE(  )  )      
  4.  
  5.   #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 'DATEDIFF(STR_TO_DATE('2/15/08','%m/%d/%y'), CURDATE())' at line 1
Member
 
Join Date: Mar 2008
Posts: 90
#8: Mar 19 '08

re: SQL Date Calculations


Quote:

Originally Posted by arggg

I made a mistake it should be

Expand|Select|Wrap|Line Numbers
  1. STR_TO_DATE(field_name, '%m/%d/%y')
however it doesnt seem to be returning anything. When I just run DATEDIFF inside of phpMyAdmin SQL it gives me a syntax error

Expand|Select|Wrap|Line Numbers
  1. Error
  2.  
  3.               DATEDIFF( STR_TO_DATE(  '2/15/08',  '%m/%d/%y'  ) , CURDATE(  )  )      
  4.  
  5.   #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 'DATEDIFF(STR_TO_DATE('2/15/08','%m/%d/%y'), CURDATE())' at line 1

Ok I got it to work the CURDATE should of been the first param. So now I have
Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT * FROM opencases_daily INNER JOIN tsms WHERE ".
  2.                 "DATEDIFF(CURDATE(),STR_TO_DATE(Last_Out_Note, '%m/%d/%y')) >= 6 ".
  3.                 "AND Assigned_To=tsms.Name AND tsms.uid='".$_REQUEST[1].
  4.                 "' ORDER BY CAST(Last_Out_Note AS UNSIGNED) DESC";
Thanks for your help!
Member
 
Join Date: Mar 2008
Posts: 90
#9: Mar 19 '08

re: SQL Date Calculations


Quote:

Originally Posted by arggg

Ok I got it to work the CURDATE should of been the first param. So now I have

Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT * FROM opencases_daily INNER JOIN tsms WHERE ".
  2.                 "DATEDIFF(CURDATE(),STR_TO_DATE(Last_Out_Note, '%m/%d/%y')) >= 6 ".
  3.                 "AND Assigned_To=tsms.Name AND tsms.uid='".$_REQUEST[1].
  4.                 "' ORDER BY CAST(Last_Out_Note AS UNSIGNED) DESC";
Thanks for your help!

is there a way to order by the datediff?
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#10: Mar 19 '08

re: SQL Date Calculations


Quote:

Originally Posted by arggg

is there a way to order by the datediff?

simplified version
Expand|Select|Wrap|Line Numbers
  1. SELECT *, DATEDIFF(CURDATE(),STR_TO_DATE(data, '%m/%d/%y')) as diff FROM a where DATEDIFF(CURDATE(),STR_TO_DATE(data, '%m/%d/%y')) >= 6 order by diff;
Ronald
Member
 
Join Date: Mar 2008
Posts: 90
#11: Mar 19 '08

re: SQL Date Calculations


Quote:

Originally Posted by ronverdonk

simplified version

Expand|Select|Wrap|Line Numbers
  1. SELECT *, DATEDIFF(CURDATE(),STR_TO_DATE(data, '%m/%d/%y')) as diff FROM a where DATEDIFF(CURDATE(),STR_TO_DATE(data, '%m/%d/%y')) >= 6 order by diff;
Ronald

ahhh thanks thats what I had i just was putting it in the wrong place!
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#12: Mar 19 '08

re: SQL Date Calculations


Ok, all solved? Then see you around some time.

ROnald
Reply


Similar MySQL Database bytes