Converting varchar to datetime type [duplicate] - mysql

This question already has answers here:
Converting a date in MySQL from string field
(4 answers)
Closed 9 years ago.
I need help with converting varchar type column which is having date time values to DATETIME type and I need only the Time part from it. i.e. I've a column with values like:
Verified_date
04/04/2013 03:17:08 PM
05/04/2013 10:10:13 AM
04/04/2013 03:45:15 PM
I need only the time part, i.e,
Time
03:17:08 PM
10:10:13 AM
03:45:15 PM

You can use this query using DATE_FORMAT:
Select DATE_FORMAT(STR_TO_DATE(('04/04/2013 03:17:08 PM'), '%d/%m/%Y %r '), '%r')
This statement will give you the required formatted date as 03:17:08 PM.
You can view the SQL Fiddle.

Try this query, just add ending formatting -
SELECT TIME(STR_TO_DATE('04/04/2013 03:17:08 PM', '%d/%c/%Y %h:%i:%s %p'));
+----------+
| 15:17:08 |
+----------+
One more simple solution -
SELECT SUBSTRING_INDEX('04/04/2013 03:17:08 PM', ' ', -2);
+-------------+
| 03:17:08 PM |
+-------------+

Try this
select DATE_FORMAT(STR_TO_DATE(start, '%d/%m/%Y %r'), '%r') from your_table;
Refer date-and-time-functions

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

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

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