how to change default date format when creating table in MYSQL - 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)

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.

Error: Update Varchar values to Datetime in MySQL

DB DETAIL Table name(KK)-
id name date(varchar(50))
1 Ayush 2020-04-19T18:56:09.774Z
I am using this query to convert -
update KK set date=DATE_FORMAT(STR_TO_DATE( KK.date, '%Y-%m-%dT%H:%i:%s' ), '%Y-%m-%d %T') where id=1;
Getting this error
Error Code: 1292. Truncated incorrect datetime value: '2020-04-19T18:56:09.774Z'
Your current date string has .774Z following the seconds, but your STR_TO_DATE() format string doesn't account for it. So it's warning you that there are extra characters at the end of the string that weren't parsed.
If you use '%Y-%m-%dT%H:%i:%s.%fZ' the warning stops.
The times have two parts your format is missing: milliseconds, the .774 part, and the "zulu" time zone Z.
select STR_TO_DATE('2020-04-19T18:56:09.774Z', '%Y-%m-%dT%H:%i:%s.%fZ');
However, since you're truncating them anyway, I'd use the less restrictive format and just ignore the warning.
Since the string is already in ISO 8601 format, you can skip the str_to_date.
mysql> select DATE_FORMAT('2020-04-19T18:56:09.774Z', '%Y-%m-%d %T');
+--------------------------------------------------------+
| DATE_FORMAT('2020-04-19T18:56:09.774Z', '%Y-%m-%d %T') |
+--------------------------------------------------------+
| 2020-04-19 18:56:09 |
+--------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
The MySQL warning is because it doesn't seem to understand that Z is a valid time zone designator.
Note that this will account for a time zone. This may or may not be what you want.
-- date_format will display in the current time zone.
-- I'm in -07:00, so a +00:00 (UTC) date is displayed -7 hours.
mysql> select DATE_FORMAT('2020-04-19T18:56:09.777+00:00', '%Y-%m-%d %T');
+-------------------------------------------------------------+
| DATE_FORMAT('2020-04-19T18:56:09.777+00:00', '%Y-%m-%d %T') |
+-------------------------------------------------------------+
| 2020-04-19 11:56:09 |
+-------------------------------------------------------------+
1 row in set (0.00 sec)
And, finally, consider altering that column to be a datetime type. Then everything is stored in UTC and these conversion problems go away.

How to convert 01:30 pm to 13:30:00 in MYSQL

I used MYSQL TIME() function to change the time format. But it gives only the time, not recognize the am or pm.
I got 01:30:00 from TIME('01:30 pm'). I want to achieve it only in MYSQL. No PHP or others. Please advice.
Thats not a real time you are storing in mysql since you also have am/pm
You first need to convert to real datetime using str_to_date and then use time_format
mysql> select time_format(str_to_date('01:30 pm','%h:%i %p'),'%H:%i:%s') as time ;
+----------+
| time |
+----------+
| 13:30:00 |
+----------+
1 row in set (0.00 sec)
Use this function TIME_FORMAT(time,format)

MySQL TIMEDIFF(DATE, DATETIME)

I have two columns in MySQL database.
One is in DATE format like 2014-01-26, another one is in DATETIME format: 2014-01-25 17:19:07.
I need to apply TIMEDIFF(2014-01-26, 2014-01-25 17:19:07) function, but it requires both variables are in DATETIME format. How can I convert 2014-01-26 to 2014-01-26 00:00:00?
You can always cast a Date to a datetime
select timediff(cast(<yourDateColumn> as Datetime), <yourDatetimeColumn>)
But I'm not even really sure that you need to cast (depending on your mysql version), I may misunderstand the doc, but we can read
Prior to MySQL 5.1.18, when DATE values are compared with DATETIME
values, the time portion of the DATETIME value is ignored, or the
comparison could be performed as a string compare. Starting from MySQL
5.1.18, a DATE value is coerced to the DATETIME type by adding the time portion as '00:00:00'. To mimic the old behavior, use the CAST()
function to cause the comparison operands to be treated as previously.
For example:
You can use date_format()
mysql> select
TIMEDIFF(date_format('2014-01-26','%Y-%m-%d %H:%i:%s'), '2014-01-25 17:19:07')
as diff;
+----------+
| diff |
+----------+
| 06:40:53 |
+----------+
1 row in set (0.00 sec)
mysql> select date_format('2014-01-26','%Y-%m-%d %H:%i:%s') as date;
+---------------------+
| date |
+---------------------+
| 2014-01-26 00:00:00 |
+---------------------+
1 row in set (0.00 sec)

Converting epoch number to human readable date in mysql

I have a epoch number say 1389422614485.
The datatype for the value storing this value is varchar.
I want to convert its value to human readable time.
How can we do it?
Any example for this conversion?
Your epoch value 1389422614485 seems like having the millisecond precision. So you need to use some mysql mathematical functions along with from_unixtime() for generating human readable format.
mysql> select from_unixtime(floor(1389422614485/1000));
+------------------------------------------+
| from_unixtime(floor(1389422614485/1000)) |
+------------------------------------------+
| 2014-01-11 12:13:34 |
+------------------------------------------+
Update July 2020: As of MySQL 8.0, the floor function is no longer necessary when working with milliseconds:
mysql> select from_unixtime(1594838230234/1000);
+------------------------------------------+
| from_unixtime(1594838230234/1000) |
+------------------------------------------+
| 2020-07-15 18:37:10.2340 |
+------------------------------------------+
Take a look at from-unixtime
mysql> SELECT FROM_UNIXTIME(1196440219);
-> '2007-11-30 10:30:19'
You can use from_unixtime() as follows:
SELECT from_unixtime(1388618430);
which returns 2014-01-02 00:20:30
This wil work for both +positive and -negative epoch, in-case for old birth dates, and also if you want to specify date format
select
date_format(DATE_ADD(from_unixtime(0), interval '1389422614485'/1000 second), '%Y-%m-%d %H:%i:%s') as my_new_date;