i have a date in this format
May 30 2006 12:00AM
is there a equivalent of strtotime() in mysql that will let me convert this to a mysql date?
I think you are looking for the STR_TO_DATE function. Unfortunately, it is not quite as awesome as PHP's strtotime, so you have to give it a format mask to parse:
mysql> SELECT STR_TO_DATE('04/31/2004', '%m/%d/%Y');
-> '2004-04-31'
For your date, I think the mask would be %M %e %Y %l:%i%p, depending on whether or not you are expecting short/long month names and 0-based days. Based on your example it could be either:
mysql> SELECT STR_TO_DATE('May 30 2006 12:00AM', '%M %e %Y %l:%i%p');
+--------------------------------------------------------+
| STR_TO_DATE('May 30 2006 12:00AM', '%M %e %Y %l:%i%p') |
+--------------------------------------------------------+
| 2006-05-30 00:00:00 |
+--------------------------------------------------------+
1 row in set (0.00 sec)
Check out the full reference table for the mask options.
You can use STR_TO_DATE (http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_str-to-date).
The format should be something like:
"%b %d %Y %l:%i%p"
Related
I have a table which pulls data from RSS Feeds and saves. The RSS feed format of representing publication date and time seems difficult to be converted into a MYSQL DATE(TIME) format. It comes as Thu, 14 Jan 2021 17:11:05 +0000 for example. So when I use STR_TO_DATE to try converting it, it returns null since STR_TO_DATE doesn't go with such formats. Also, I can't even trim parts of the string and make it work with STR_TO_DATE. Of course, other functions like UNIX_TIMESTAMP won't work with that kind of string. How can I convert such a complex string to a MYSQL date format? I'm trying to do something like:
SELECT * FROM tbl WHERE 1 ORDER BY STR_TO_DATE('Thu, 14 Jan 2021 17:11:05 +0000', '%d-%m-%Y')
You need to complete your date format.
You can truncate the parts you don't want afterwards.
As an alternative you could use substring before convertion.
select str_to_date(
'Thu, 14 Jan 2022 17:11:05 +0000',
'%a, %d %b %Y %H:%i:%s +%f') formatted_date
| formatted_date |
| :------------------------- |
| 2022-01-14 17:11:05.000000 |
db<>fiddle here
In a table say testTable we are storing date-time as varchar in following format
Tue May 09 15:16:54 IST 2017
I am trying to write a query which gives me all records between two dates using STR_TO_DATE to convert the date in varchar format to datetime. However below query is failing with Error Code: 1241 Operand should contain 1 column(s)
SELECT * FROM test12.testTable a WHERE a.timestamp BETWEEN (STR_TO_DATE('Tue May 09 17:26:11 IST 2017', '%a %b %d %H:%i:%s %Z %Y'),
STR_TO_DATE('Wed May 10 20:17:11 IST 2017', '%a %b %d %H:%i:%s %Z %Y'));
Could you suggest what is wrong here?
When your dates are stored in this weird way, you want to use the STR_TO_DATE() function on the column, not on the string you provide.
SELECT * FROM test12.testTable a
WHERE STR_TO_DATE(a.timestamp, '%a %b %d %H:%i:%s %Z %Y')
BETWEEN '2017-05-09 17:26:11' AND '2017-05-10 20:17:11';
Appreciate that this topic has been covered many times and I have tried all the combinations I can find without success.
The following timestamp is an example of that returned when using rpt_default_day.time_stamp:
1474502400000
If I put this time stamp into the following website it returns the correct date and time:
http://www.epochconverter.com/
Below are some examples of queries I have been using:
DATE_FORMAT(FROM_UNIXTIME('rpt_default_day.time_stamp'), '%e %b %Y') AS 'Date',
FROM_UNIXTIME('rpt_default_day.time_stamp') AS 'Date',
FROM_UNIXTIME(UNIX_TIMESTAMP('rpt_default_day.time_stamp')) AS 'Date',
Problem is whatever I do I'm always getting returned the epoch time of:
'1970-01-01 01:00:00.000000'
Appreciate any help in advance.
Remove three zeros from your string and you're good to go.
A proper format values should be passed as a parameter to "FROM_UNIXTIME" function :
mysql> SELECT UNIX_TIMESTAMP(NOW()) as ts,
FROM_UNIXTIME(UNIX_TIMESTAMP(NOW()), '%Y %D %M %h:%i:%s %x') as f_tm;
+------------+-----------------------------------+
| ts | f_tm |
+------------+-----------------------------------+
| 1474755927 | 2016 25th September 01:25:27 2016 |
+------------+-----------------------------------+
1 row in set (0.06 sec)
possible format values are specified in the Mysql Documentation
select COUNT(DISTINCT devices) AS "Devices" from measure_tab where
measure_tab.time >= 1375243200 and
measure_tab.time < 1375315200;
The output of the above sql query gives a table with a column named "Devices" with number of devices. I want the time which is one of the attributes of measure_tab should also get displayed in another column of the output table with the UNIX TIME which is 1375243200 in this query converted into the SQL datetime format which is Thu Aug 1 00:00:00 UTC 2013.
Can someone help me out?
Thanks
You can use function as below:
select FROM_UNIXTIME(UNIX_TIMESTAMP(),'%a %b %d %H:%i:%s UTC %Y');
output will be:
'Wed Feb 05 05:36:16 UTC 2014'
In your query
select COUNT(DISTINCT devices) AS "Devices",
FROM_UNIXTIME(measure_tab.time,'%a %b %d %H:%i:%s UTC %Y') as d from measure_tab where
measure_tab.time >= 1375243200 and
measure_tab.time < 1375315200;
For more info you can check documentation: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
You can see sql fiddle:http://sqlfiddle.com/#!2/a2581/20357
In Mysql you can use from_unixtime() function to convert unix timestamp to Date:
select COUNT(DISTINCT devices) AS "Devices" from measure_tab where
measure_tab.time >= from_unixtime(1375243200) and
measure_tab.time < from_unixtime(1375315200);
you could use FROM_UNIXTIME inside DATE_FORMAT, but luckily,
FROM_UNIXTIME also accepts a format string, so you could just use it
by itself
Like this
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(),'%Y %D %M %h:%i:%s %x')
DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')
DATE_FORMAT(NOW(),'%m-%d-%Y')
DATE_FORMAT(NOW(),'%d %b %y')
DATE_FORMAT(NOW(),'%d %b %Y %T:%f')
As detailed in the other answers, FROM_UNIXTIME is the function you are looking for. Please be aware that this implicitly takes into account the local time zone setting on the machine running MySQL.
There's lots of useful information here:
Should MySQL have its timezone set to UTC?
if you need to find out how to check/set the time zone.
The year is expected to be picked as current year instead of the 00 year as shown below.
mysql> select str_to_date('Jul 15 12:12:51', '%b %e %T');
+--------------------------------------------+
| str_to_date('Jul 15 12:12:51', '%b %e %T') |
+--------------------------------------------+
| 0000-07-15 12:12:51 |
+--------------------------------------------+
Expected result:
| 2013-07-15 12:12:51 |
Well, that's intended. Quoting the doc:
Unspecified date or time parts have a value of 0, so incompletely
specified values in str produce a result with some or all parts set to
0. [...] “Zero” dates or dates with part values of 0 are permitted unless the SQL mode is set to disallow such values.
It's easy to fix, btw:
select str_to_date(CONCAT(YEAR(NOW()), ' ', 'Jul 15 12:12:51'), '%Y %b %e %T');
-- July, 15 2013 12:12:51+0000
Simple, ugly, but works:
select str_to_date(concat('Jul 15, ', year(now()), ' 12:12:51'), '%b %e, %Y %T');