473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

SQL Date Calculations

91
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.
Mar 18 '08 #1
11 3887
ronverdonk
4,258 Expert 4TB
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
Mar 18 '08 #2
arggg
91
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?
Mar 18 '08 #3
ronverdonk
4,258 Expert 4TB
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
Mar 18 '08 #4
arggg
91
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
Mar 18 '08 #5
ronverdonk
4,258 Expert 4TB
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
Mar 18 '08 #6
arggg
91
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
Mar 18 '08 #7
arggg
91
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!
Mar 18 '08 #8
arggg
91
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?
Mar 18 '08 #9
ronverdonk
4,258 Expert 4TB
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
Mar 19 '08 #10
arggg
91
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!
Mar 19 '08 #11
ronverdonk
4,258 Expert 4TB
Ok, all solved? Then see you around some time.

ROnald
Mar 19 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Marc | last post by:
I've got a PHP script that's using fopen to connect to a remote web server and pick up data. I've run into a problem in calculating elapsed time. The remote web server outputs a time and I'd...
13
by: perplexed | last post by:
How do you convert a user inputted date to a unix timestamp before insterting it into your database? I have a form, with a textfield for a date that the user inputs in the format mm-dd-yyyy and...
2
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much...
30
by: Dr John Stockton | last post by:
It has appeared that ancient sources give a method for Numeric Date Validation that involves numerous tests to determine month length; versions are often posted by incomers here. That sort of code...
11
by: lduperval | last post by:
Hi, I`m trying to do date calculations in three types of time zones: local, GMT and specified. The issue I am facing is that I need to be able to specify a date in the proper time zone, and I`m...
2
by: Joe Jax | last post by:
I have some date calculations that add a time span to a date. The problem is, when I add a time span that is a whole number of days to a date, the result can be +/- 1 hour due to daylight savings....
1
by: bluerocket | last post by:
I have searched this group, and am not finding the answer I am looking for -- hope you can help. I have a front-end MS Access database hooked via a MyODBC link to a MySQL database. A modified...
5
by: Simon Dean | last post by:
Probably being a little thick here, but when you subtract one date away from another, how do you convert the resultant value into a number of days... I guess I could easily / 60 / 60 / 24... but...
3
by: Glencannon4424 via AccessMonster.com | last post by:
Hello, I have what I believe amounts to a Date Serial issue. I have the following columns in my table: Hire-Date Hire-Year WTD-RATE Wks-in-Yr
8
by: Charlie Brookhart | last post by:
I am creating a program that involves having to find the difference between two dates and converting it to a number to be used for calculations. The problem is that the way it is setup, VB is not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.