In my SQL database Valid Up To : Thursday, January 01, 2015 3:17 AM is stored as string with column name valid time. Now I want to select data which are updated in last one hour. How can I make a suitable query?
You can parse the string and convert it to a date with STR_TO_DATE(). Parsing options are described here.
SELECT
*
FROM your_table
WHERE STR_TO_DATE(`valid time`, 'Valid Up To : %W, %M %d, %Y %l:%m %p') >= NOW() - INTERVAL 1 HOUR;
Related
I have CHAR strings stored in the database field in the format mm/dd/yyyy. Such as
2/26/2022
2/19/2022
2/12/2022
2/5/2022
12/31/2021
12/18/2021
11/27/2021
I need to sort them as shown according to the "date" without changing the declaration.
The post at MySQL date format DD/MM/YYYY select query? suggested using ORDER BY STR_TO_DATE(datestring, '%d/%m/%Y')
My MySQL statement looks like this:
SELECT stringdate
FROM mytable
WHERE product = '#myproduct#'
ORDER BY STR_TO_DATE(stringdate, '%m/%d/%y') DESC
However, the result is not sorted properly. Instead of the desired order as shown above, it is showing like this:
12/31/2021
12/18/2021
11/27/2021
2/26/2022
2/19/2022
2/12/2022
2/5/2022
It seems that the year is being ignored. How can I sort this without actually changing the database field declaration?
Thanks in advance.
2/5/2022 is month and day without leading zeros, and four digit year. The format string you have specified is -
%m - Month, numeric (00..12)
%d - Day of the month, numeric (00..31)
%y - Year, numeric (two digits)
SELECT stringdate
FROM mytable
WHERE product = '#myproduct#'
ORDER BY STR_TO_DATE(stringdate, '%c/%e/%Y') DESC
%c - Month, numeric (0..12)
%e - Day of the month, numeric (0..31)
%Y - Year, numeric, four digits
Executing the following query shows the difference in the converted dates -
SELECT
stringdate,
STR_TO_DATE(stringdate, '%m/%d/%y'),
STR_TO_DATE(stringdate, '%c/%e/%Y')
FROM mytable
WHERE product = '#myproduct#'
ORDER BY STR_TO_DATE(stringdate, '%c/%e/%Y') DESC
db<>fiddle
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format
%y is the two-digit year code. So you are sorting them all as '20'
%Y is the four-digit year code.
See reference for the date format codes here: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format
I recommend you use the DATE data type instead of CHAR.
The datatype of date column in my table is char(10). So each date is stored as a string like '02/01/2016/'.
How can I get the day of week and month of year from this '02/01/2016' in mysql?
Convert the string to DATE datatype using STR_TO_DATE function, and then use DATE_FORMAT function.
SELECT DATE_FORMAT( STR_TO_DATE( '02/01/2016', '%m/%d/%Y'), '%w') AS dow
, DATE_FORMAT( STR_TO_DATE( '02/01/2016', '%m/%d/%Y'), '%c') AS moy
(The format specifier needs to match the format of the string. This demonstration assumes that the string is in month/day/year format, that this represents February 1st, and not January 2nd.)
If you want to return a string like 'Monday', use '%W' in place of '%w'
MySQL Reference Manual:
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_str-to-date
As #paxdiablo commented, you should seriously consider storing your date information as date, datetime, or timestamp. That being said, if you must live with your current setup, you can work around this by first parsing your text into a date using STR_TO_DATE() and then extracting out a string weekday and month name using DATE_FORMAT(). Something like this should work:
SELECT DATE_FORMAT(STR_TO_DATE(date, '%d/%m/%Y'), '%W') AS day_of_week,
DATE_FORMAT(STR_TO_DATE(date, '%d/%m/%Y'), '%M') AS month_of_year
FROM yourTable
Demo here:
SQLFiddle
I have a table which contains a varchar field containing date like '15 May 2015 - 03:10 am'
I have to compare all date in this table with the current date to retrieve row which are next the current date and row which are recent to the current date
I found this solution here:
select *
from reunion a
where STR_TO_DATE(a.dateDebut,'%d %b %Y - %I:%i %p')>
DATE_FORMAT(NOW(),'%d %b %Y - %I:%i %p');
but it returns all date: '15 May 2013 - 03:10 AM' ,'15 May 2015 - 03:10 am','15 May 2016 - 03:10 am'
and when I change to "<" it returns 0 rows;
is there any other solution to give the varchar field the ability to be a valid date?
Your query is overkill. You just need to convert your date format to a date. now() is already a date. So try:
select *
from reunion a
where STR_TO_DATE(a.dateDebut,'%d %b %Y - %I:%i %p') > NOW();
Hello all,
This is the format of my my-sql data type "rdate".
Apr 1 2011 01:13:00:000PM
I want to use the order by rdate and i can't make it right order as the data type of rdate is varchar, So i want to convert it to date time , But no success.
I am trying to use date_format(str_to_date(rdate, '%m/%d/%Y'), '%Y%m');
Thanks
Mypixel
Try doing:
ORDER BY str_to_date(rdate,'%M %d %Y %h:%i:%s')
From the docs:
Your Date is in the Following format:
%M Month name (January..December)
%d Day of the month, numeric (00..31)
%Y Year, numeric, four digits
...
You have to tell str_to_date the format that your string is in. This means the way the specific parts of the date are displayed, spaces, etc.
sqlfiddle demo
In your str_to_date function call, you need to specify what the format IS, not what you want it to be. Try this:
str_to_date(rdate, '%M %d %Y %h:%i:%s'));
UPDATE table SET rdate=str_to_date(rdate,'%M %d %Y %h:%i:%s')
Just convert your column for good to datetime.
How do I convert the following format to unix timestamp?
Apr 15 2012 12:00AM
The format I get from DB seems to have AM at the end.
I've tried using the following but it did not work:
CONVERT(DATETIME, Sales.SalesDate, 103) AS DTSALESDATE,
CONVERT(TIMESTAMP, Sales.SalesDate, 103) AS TSSALESDATE
where Sales.SalesDate value is Apr 15 2012 12:00AM
Here's an example of how to convert DATETIME to UNIX timestamp:
SELECT UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2012 12:00AM', '%M %d %Y %h:%i%p'))
Here's an example of how to change date format:
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2012 12:00AM', '%M %d %Y %h:%i%p')),'%m-%d-%Y %h:%i:%p')
Documentation: UNIX_TIMESTAMP, FROM_UNIXTIME
You will certainly have to use both STR_TO_DATE to convert your date to a MySQL standard date format, and UNIX_TIMESTAMP to get the timestamp from it.
Given the format of your date, something like
UNIX_TIMESTAMP(STR_TO_DATE(Sales.SalesDate, '%M %e %Y %h:%i%p'))
Will gives you a valid timestamp. Look the STR_TO_DATE documentation to have more information on the format string.
If you want to create a timestamp as returned by java's Date.getTime() you should multiply by 1000.
SELECT UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2012 12:00AM', '%M %d %Y %h:%i%p'))*1000
Now for a more standard date format use:
SELECT UNIX_TIMESTAMP(STR_TO_DATE('2022-12-14 20:58:00', '%Y-%m-%d %H:%i:%s'))*1000
From http://www.epochconverter.com/
SELECT DATEDIFF(s, '1970-01-01 00:00:00', GETUTCDATE())
My bad, SELECT unix_timestamp(time) Time format: YYYY-MM-DD HH:MM:SS or YYMMDD or YYYYMMDD. More on using timestamps with MySQL:
http://www.epochconverter.com/programming/mysql-from-unixtime.php