Convert MySQL Unix Time Stamp to Human Readble Form - mysql

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

Related

convert date into mysql datetime format

I have datetime in the following format in a csv data
In_Time
1/1/17 1:07 AM
1/1/17 12:59 PM
I am unable to load data with a column that holds values like above. SO I loaded the data in text format for the In_Time column and I am trying to use STR_TO_DATE() function to now convert the column into a datetime column in mysql.
I am trying the below code but it gives me error:
Incorrect datetime value: '1/1/17 12:27 AM' for function str_to_date
UPDATE mytable
SET In_Time = STR_TO_DATE(ED_Arrival_Time, '%d/%m%y %h: %i: %p');
Please help.
There is no such time as 12:59 PM however given the correct formatting options for str_to_date https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format for the dates passed the result will be null and no error is thrown
DROP TABLE IF EXISTS T;
create table t
( ED_Arrival_Time varchar(20), In_Time datetime);
insert into t values
('1/1/17 1:07 AM',null),('1/1/17 11:59 PM',null),('1/1/17 23:59 PM',null);
select ED_Arrival_Time, str_to_date(ed_arrival_time,'%d/%m/%y %h:%i %p')
from t;
+-----------------+--------------------------------------------------+
| ED_Arrival_Time | str_to_date(ed_arrival_time,'%d/%m/%y %h:%i %p') |
+-----------------+--------------------------------------------------+
| 1/1/17 1:07 AM | 2017-01-01 01:07:00 |
| 1/1/17 11:59 PM | 2017-01-01 23:59:00 |
| 1/1/17 23:59 PM | NULL |
+-----------------+--------------------------------------------------+
3 rows in set, 1 warning (0.001 sec)
The expectation is that all the incoming dates are in the same format - if not then you need to cleanse them.
BTW load data infile can manipulate data loading from a csv file see the section Input Preprocessing in the manual https://dev.mysql.com/doc/refman/8.0/en/load-data.html
As a general principle, it's good to break the problem down into the smallest part that is causing a problem and solve that. In this case, I think that is the format specifier for the STR_TO_DATE() function.
Find a list of format specifiers: https://www.w3schools.com/sql/func_mysql_str_to_date.asp
Open a MySQL terminal, then iteratively try it with a few of your strings until you get the correct format specifier string which should be something like this:
select str_to_date('1/1/17 1:07 AM', '%e/%c/%y %I:%i %p') as test_date_parse;
Then adjust your code with the correct date specifier.

Trouble with DATE_FORMAT and STR_TO_TIME with AM/PM

I need to get the 24 hour time of a string, but I can only get the 12 hour for some reason using Mysql.
SELECT STR_TO_DATE('3/13/2018 9:28:07 PM', '%m/%d/%Y %T');
+-----------------------------------------------------+
| STR_TO_DATE('3/13/2018 9:28:07 PM', '%m/%d/%Y %T') |
+-----------------------------------------------------+
| 2018-03-13 09:28:07 |
+-----------------------------------------------------+
I have tried a variety of methods and thought it was working correctly, which it does, before noon....
I am trying to use it to limit the returned results to only things that have changed since the last time I ran the query.
%T is for time in 24 hour notation, so STR_TO_DATE is ignoring the PM/AM part of your time. You need to use %r. See the manual for details.
You need to convert it to datetime with time zone then use date format %T.
select DATE_FORMAT(STR_TO_DATE('3/13/2018 9:28:07 PM', '%m/%d/%Y %r'), '%T')
21:28:07

varchar date time comparison issue

I have a legacy table which has a varchar column represent date, format is MM/DD/YYYY (e.g. 01/08/2015). It is not convenient to perform data range selection since it is a varchar (when I use < or > kinds comparison, it goes to varchar/string comparison, which have different results from date comparision).
For example, I want to select only rows which dates are between 01/08/2015 and 01/10/2015. Any smart solution is appreciated, and I cannot change the data type of varchar to date in my existing table.
I am using MySQL Workbench/MySQL.
Varchar dates are evil and they are not real date, the best solution is to use mysql's native date data types.
Since you can't change the datatype you may use str_to_date() function and here how it works
mysql> select str_to_date('01/08/2015','%d/%m/%Y') as d ;
+------------+
| d |
+------------+
| 2015-08-01 |
+------------+
So the query for select would be
select * from table_name
where
str_to_date(date_column,'%d/%m/%Y')
between
str_to_date('01/08/2015','%d/%m/%Y')
and
str_to_date('01/10/2015','%d/%m/%Y')
There are many answers which addresses many different way of converting the string to date.
You may choose whichever is perfect for your need
SELECT * FROM `your_table` WHERE DATE_FORMAT(my_column_with_the_string_date, "%Y-%m-%d") <= '2011-09-30'
DATE_FORMAT can be used to convert your date string to any format: I will use the NOW() function instead of string to list different
formats that are supported
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')
The output of the above is:
Nov 04 2014 11:45 PM
11-04-2014
04 Nov 14
04 Nov 2014 11:45:34:243
You can modify your query accordingly
You can cast your dates as strings using STR_TO_DATE:
STR_TO_DATE(yourdatefield, '%m/%d/%Y')
SELECT STR_TO_DATE(got_fired_at, '%m/%d/%Y') BETWEEN ? AND ? FROM firings;
(field/table names guaranteed to have been chosen randomly)
Use MySQL's STR_TO_DATE function to parse the date strings to date objects then do the comparison.

Converting multiple datetime formats to single mysql datetime format

I hired a freelancer a little while ago to parse a website which had a datetime field. I had to put the project on the back burner shortly after the freelancer completed and now that I'm getting back into it, I've noticed some issues when I go into the MySQL database.
Specifically, there are two different datetime formats and I can't figure out how to update them into a unified MySQL sortable datetime field
Apr 8 - 2:23 AM <- for current year updates
Tue, Dec 2, 2014 06:06:00 PM <- for all previous year updates
2014-12-02 06:06:00PM <- desired format
I have a unique id in the table so I can select and update the formats easily. All "Apr 8 - 2:23 AM" format is < '6340' for example. I also have created a "date_proper" column to update the current date column.
I just can't for the life of me figure out the correct code to update the different formats into the same unified format. Any help would be much appreciated.
You need to use str_to_date() for this. First, fix the date_proper column so it is a datetime. Formats should be handled on input and output. The proper storage for dates is using native formats:
alter table modify date_proper datetime;
Then you can update the values:
update t
date_proper = (case when format < '6340'
then str_to_date(concat(year(now), ' ', col), '%Y %b %d - %h:%i %p'
else str_to_date(substr(col, 5), '%b %d, %Y %h:%i:%s %p'
end);

MySQL Update from mm/dd/YY h:i:s A to YY-mm-dd H-i-s

I recently change the data type of my field from varchar to datetime, but before I did that, I made a copy of it first because the content will reset to this value 000-00-00 00-00-00
And now, I am trying to update the datetime field of my table.
Here's what I did
UPDATE table
INNER JOIN copyOfTable ON (table.logID = copyOfTable.logID)
SET table.date = DATE(STR_TO_DATE(copyOfTable.date, '%m/%d/%Y'))
The result goes something like this
YYYY-mm-dd 00-00-00
2013-08-02 00-00-00
I also tried
SET table.date = DATE(STR_TO_DATE(copyOfTable.date, '%m/%d/%Y %h:%i:%s'))
and the result remains the same.
What I want is to copy the time also (hh:ii:ss)
so that the total format of time is (YY-mm-dd hh-ii-ss)
The format of time from table 'copyOfTable' is (mm/dd/yy h:i:s A)
How to do this guys?
IMHO
don't wrap STR_TO_DATE() into DATE() if you need time portion of your datetime values to be preserved
your format string should be '%m/%d/%y %h:%i:%s %p'
According to you
The format of time from table 'copyOfTable' is (mm/dd/yy h:i:s A)
so if I understand correctly your values in copyOfTable look like 08/01/13 04:08:12 AM
Here is an example
SELECT STR_TO_DATE('08/01/13 04:08:12 AM', '%m/%d/%y %h:%i:%s %p') new_date
Output:
| NEW_DATE |
---------------------------------
| August, 01 2013 04:08:12+0000 |
Here is SQLFiddle demo