Convert datetime column to 24 hour format - mysql

I have a column called schedule_time (datetime) format. I want to convert the time to 24 hour time.
2016-03-08 03:00:00 to 2016-03-08 15:00:00

Please note that DATETIME values are always stored in 24h format (http://dev.mysql.com/doc/refman/5.7/en/datetime.html). There is no AM/PM.
When you want to display the values, there is however the DATE_FORMAT
function, which will format the value according to your needs, including AM/PM:
select DATE_FORMAT(schedule_time, '%Y-%m-%d %h:%i:%s %p') from t1;
This will give 2016-03-08 03:00:00 AM and 2016-03-08 03:00:00 PM. But the values in the DB are still the same, in 24h format.
If adding 12 hours would solve your issue, the you can do it like this:
start transaction;
update t1 set schedule_time = date_add(schedule_time, interval 12 hour);
select * from t1; -- verify!!!
rollback;
-- or commit;
I put this in a transaction so you can first verify your results. If they are wrong, simply rollback the transaction (provided you use InnoDB tables). If you don't have transactions (or feel uncomfortable with them), you can undo the change with date_sub instead of date_add.
But be aware: This doesn't change from 12h to 24h format, it simply adds 12 hours to all your schedule_time values.

Use MySQL's DATE_FORMAT function.
The format string will be '%Y-%m-%d %T'.
Selecting the current date with 24-hour time:
mysql> SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %T') AS now
+---------------------+
| now |
+---------------------+
| 2016-03-08 20:47:04 |
+---------------------+
1 row in set (0.00 sec)
Selecting a date with 24-hour time from a table:
mysql> SELECT DATE_FORMAT(`created_at`, '%Y-%m-%d %T') AS created_at FROM test.comments;
+---------------------+
| created_at |
+---------------------+
| 2016-02-25 16:32:12 |
+---------------------+
1 row in set (0.00 sec)

Related

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.

DATE_FORMAT not picking my format in mysql

this is my query (Not Working): SELECT date_format('21-Oct-19 15:59','%Y-%m-%d %H:%i') + interval 3 day, date_format(now(), '%d-%b-%y %H:%i')
this is my query (Working): SELECT '2019-10-21 15:59' + interval 3 day, date_format(now(), '%d-%b-%y %H:%i')
My data is in this format 21-Oct-19 15:59 i want to add 3 day in it. How I can achieve this?
If you have a datetime in a non-standard format (that is, non YYYY-MM-DD HH:MM:SS), you need to convert it to a standard datetime using STR_TO_DATE().
You used DATE_FORMAT() which does the opposite — it only accepts a standard datetime, and you can format a non-standard datetime.
You also need a different format specifier corresponding to the non-standard datetime input. You don't need to specify the output format for STR_TO_DATE(). It always outputs standard datetime.
mysql> SELECT STR_TO_DATE('21-Oct-19 15:59','%y-%b-%d %H:%i') AS d;
+---------------------+
| d |
+---------------------+
| 2021-10-19 15:59:00 |
+---------------------+
mysql> SELECT STR_TO_DATE('21-Oct-19 15:59','%y-%b-%d %H:%i') + INTERVAL 3 DAY AS d;
+---------------------+
| d |
+---------------------+
| 2021-10-22 15:59:00 |
+---------------------+
You need to do the date_format after adding the interval :
SELECT date_format(now() + interval 3 day, '%d-%b-%y %H:%i') ;

How to convert UNIX time before 1970 to date format in MySQL?

I have a database using unix time for its dates ( i am using mySQL). I want to retrieve the dates in daily date format. This is my query:
SELECT FROM_UNIXTIME(time_created) FROM member
This works fine with dates after 1970 (for example, 1314162229) but doesn't work for dates before 1970 (for example, -769338000). Is there any work around here?
A possible workaround would be to have a constant handy corresponding to the seconds in a certain number of years (preferrably a multiple of 4). You could add this constant, translate the time and then subtract the number of years chosen.
Example: choose 40 years.
Determine the constant:
MySQL [files]> select adddate(from_unixtime(0), interval 40 year);
+---------------------------------------------+
| adddate(from_unixtime(0), interval 40 year) |
+---------------------------------------------+
| 2010-01-01 01:00:00 |
+---------------------------------------------+
1 row in set (0.09 sec)
MySQL [files]> select unix_timestamp(adddate(from_unixtime(0), interval 40 year));
+-------------------------------------------------------------+
| unix_timestamp(adddate(from_unixtime(0), interval 40 year)) |
+-------------------------------------------------------------+
| 1262304000 |
+-------------------------------------------------------------+
1 row in set (0.09 sec)
Now you can every unix timestamp x between 1930 and 20xx and use it.
select subdate(from_unixtime(x+1262304000), interval 40 year);
With your example -769338000, you get
MySQL [files]> select subdate(from_unixtime(-769338000+1262304000), interval 40 year);
+-----------------------------------------------------------------+
| subdate(from_unixtime(-769338000+1262304000), interval 40 year) |
+-----------------------------------------------------------------+
| 1945-08-15 17:00:00 |
+-----------------------------------------------------------------+
1 row in set (0.09 sec)
I found a new way:
converting to MySQL date:
SELECT DATE_ADD(FROM_UNIXTIME(0), interval YOURTIMESTAMPHERE second);
converting your epoch to a date string:
SELECT DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), interval YOURTIMESTAMPHERE second), '%Y-%m-%d');
And back
SELECT TIMESTAMPDIFF(second,FROM_UNIXTIME(0),'1960-01-01 00:00:00' );
source:
http://www.epochconverter.com/programming/mysql-from-unixtime.php#negavtiveEpoch
SELECT DATE_ADD(CAST('1970-01-01 00:00:00' AS DATETIME), INTERVAL `time_created` SECOND) FROM `member`
To my knowledge there is no such thing as UNIX time prior to 1/1/1970 00:00 UTC. More at Wikipedia.

Date time comparisons

I have a date column for ex: 03/08/2011 & i need to make that as end of the day ie. 03/08/2011 12:00:00 in UTC time format & then add some integer value in minutes say for ex:10 (in mins)-> 03/08/2011 12:10:00. This value is used as end value.
There will be some start value like for ex: 03/07/2011 22:10:00 & i need to get current date time in UTC, do a comparison to see & return 1 if current date time falls between start & end value else return 0.
All in a single select query in mysql.
Thanks.
It's not clear exactly what you need, but I'll give you a couple of specific examples that should help.
1) Converting a MM/DD/YYYY string to a date and adding 10 minutes to it:
mysql> SELECT STR_TO_DATE('03/08/2011', '%m/%d/%Y') + INTERVAL 10 MINUTE;
+------------------------------------------------------------+
| STR_TO_DATE('03/08/2011', '%m/%d/%Y') + INTERVAL 10 MINUTE |
+------------------------------------------------------------+
| 2011-03-08 00:10:00 |
+------------------------------------------------------------+
1 row in set (0.00 sec)
2) Getting the current timestamp in UTC:
mysql> SELECT UTC_TIMESTAMP();
+---------------------+
| UTC_TIMESTAMP() |
+---------------------+
| 2011-03-08 20:33:53 |
+---------------------+
1 row in set (0.00 sec)

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)