convert date into mysql datetime format - mysql

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.

Related

How to update a MYSQL table column from Varchar data type to a time

I have a table in an MYSQL database and one of the columns (Incident_Time) has values such as 03:15 A and 12:30 P stored as varchar data type. However, I would like to change the data type from varchar to timestamp (time) so that the values can be treated as time values. For example,time as 10:37 AM.
-------------
Incident_Time
--------------
| 10:37 A |
| 03:15 A |
| 12:20 P |
I tried the following code:
UPDATE incident_tab_22
SET Incident_Time = str_to_date(Incident_Time, '%h:%i %p');
I keep getting the following error reading:
Error 1411 (HY000): Incorrect datetime value: ‘10:37 A’ for function str_to_date
As an alternative solution, I also tried:
select *, SELECT STR_TO_DATE(Incident_Time, '%h:%i %p') ; as Time_of_Incident from incident_tab_22;
This just resulted in a column created (Time_of_Incident) with all NULL values.
I would appreciate any assistance I can get with this problem. Thanks.
To use %p you need to add an M:
select *,
STR_TO_DATE(concat(Incident_Time,'M'), '%h:%i %p') as Time_of_Incident
from incident_tab_22
fiddle

Mysql - date and time columns to datetime

I have been trying to merge date and time into datetime. My date is varchar and I need to have got one column datetime (as datetime)
My input is like that:
create table t1 (
date1 varchar (12),
time1 varchar (39));
INSERT INTO t1 VALUES
('01.Mar.2019', '11:30'),
('02.Mar.2019', '1:30'),
('03.Mar.2019', '0:30'),
('03.Mar.2019', '10:30');
And the desired results are below:
datetime1
01.Mar.2019 11:30:00
02.Mar.2019 01:30:00
03.Mar.2019 00:30:00
03.Mar.2019 10:30:00
SELECT STR_TO_DATE(CONCAT(date1,' ',time1),'%d.%b.%Y %H:%i') x FROM t1;
+---------------------+
| x |
+---------------------+
| 2019-03-01 11:30:00 |
| 2019-03-02 01:30:00 |
| 2019-03-03 00:30:00 |
| 2019-03-03 10:30:00 |
+---------------------+
4 rows in set (0.00 sec)
Although I feel that the way you are storing these values is not the best solution, you can present the data in its current stored format as a DATETIME by using CONCAT to join the separate VARCHARs together and then STR_TO_DATE to convert the string into a DATETIME:
SELECT STR_TO_DATE(CONCAT(date1, ' ', time1), '%d.%b.%Y %k:%i') FROM t1;
This is broken into two parts:
CONCAT(date1, ' ', time1)
Joins the date and time together with a space into a single string that will look like this:
01.Mar.2019 11:30
02.Mar.2019 1:30
03.Mar.2019 0:30
03.Mar.2019 10:30
Then, STR_TO_DATE converts this to a DATETIME using format specifiers:
STR_TO_DATE(CONCAT(date1, ' ', time1), '%d.%b.%Y %k:%i')
So, the formats are:
%d - Day of the month with leading zero if it is 1 number (01, 02, 03, and so on)
%b - Three-characters abbreviated month name (Jan, Feb, Mar, and so on)
%Y - Four digits year (2018, 2019, and so on)
%k - Hour in 24-hour format without leading zero (0, 1, 2, and so on)
%i - Minutes with leading zero (00, 01, 02, and so on)
The output from this will be:
2019-03-01 11:30:00
2019-03-02 01:30:00
2019-03-03 00:30:00
2019-03-03 10:30:00
I just want to add that I feel that the best way to store date and time information in the database is almost certainly to use a DATETIME column.

Convert MySQL Unix Time Stamp to Human Readble Form

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

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

how to change default date format when creating table in MYSQL

how to change default date format when creating table in MYSQL
You can't change the default format for a date during the table definition stage. (It must always obey the DATETIME, DATE or TIMESTAMP formats.) As the manual puts it:
Although MySQL tries to interpret
values in several formats, dates
always must be given in year-month-day
order (for example, '98-09-04'),
rather than in the month-day-year or
day-month-year orders commonly used
elsewhere (for example, '09-04-98',
'04-09-98').
See the date and time reference docs for more info.
As such, you'll have to use the DATE_FORMAT() function at the point of output to achieve this goal.
You may want to use the STR_TO_DATE() and DATE_FORMAT() functions to communicate with MySQL using different date formats.
Example using STR_TO_DATE():
SELECT STR_TO_DATE('15-Dec-09 1:00:00 PM', '%d-%b-%y %h:%i:%S %p') AS date;
+---------------------+
| date |
+---------------------+
| 2009-12-15 13:00:00 |
+---------------------+
1 row in set (0.07 sec)
Example using DATE_FORMAT():
SELECT DATE_FORMAT('2009-12-15 13:00:00', '%d-%b-%y %h:%i:%S %p') AS date;
+-----------------------+
| date |
+-----------------------+
| 15-Dec-09 01:00:00 PM |
+-----------------------+
1 row in set (0.00 sec)