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.
11 3887
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 - .... STR_TO_DATE(field_name, '%m/%d/%y') ....;
and then use that result to compare with the MySQL CURRDATE() values, like - SELECT * from table_name WHERE DATEDIFF(STR_TO_DATE(field_name, '%m/%d/%'), CURRDATE()) >= 6;
Ronald
You first have to rework your date from the VARCHAR field into a 'normal' MySQL date format like - .... STR_TO_DATE(field_name, '%m/%d/%y') ....;
and then use that result to compare with the MySQL CURRDATE() values, like - 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?
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
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. - 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
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 - STR_TO_DATE(field_name, '%m/%d/%y') ....;
So why are you now using - str_to_date(field_name, '%Y-%c-%e')
Ronald
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 - STR_TO_DATE(field_name, '%m/%d/%y') ....;
So why are you now using - str_to_date(field_name, '%Y-%c-%e')
Ronald
I made a mistake it should be - 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 - Error
-
-
DATEDIFF( STR_TO_DATE( '2/15/08', '%m/%d/%y' ) , CURDATE( ) )
-
-
#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
I made a mistake it should be - 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 - Error
-
-
DATEDIFF( STR_TO_DATE( '2/15/08', '%m/%d/%y' ) , CURDATE( ) )
-
-
#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 -
$sql = "SELECT * FROM opencases_daily INNER JOIN tsms WHERE ".
-
"DATEDIFF(CURDATE(),STR_TO_DATE(Last_Out_Note, '%m/%d/%y')) >= 6 ".
-
"AND Assigned_To=tsms.Name AND tsms.uid='".$_REQUEST[1].
-
"' ORDER BY CAST(Last_Out_Note AS UNSIGNED) DESC";
Thanks for your help!
Ok I got it to work the CURDATE should of been the first param. So now I have -
$sql = "SELECT * FROM opencases_daily INNER JOIN tsms WHERE ".
-
"DATEDIFF(CURDATE(),STR_TO_DATE(Last_Out_Note, '%m/%d/%y')) >= 6 ".
-
"AND Assigned_To=tsms.Name AND tsms.uid='".$_REQUEST[1].
-
"' ORDER BY CAST(Last_Out_Note AS UNSIGNED) DESC";
Thanks for your help!
is there a way to order by the datediff?
is there a way to order by the datediff?
simplified version - 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
simplified version - 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!
Ok, all solved? Then see you around some time.
ROnald
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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...
| |