Trouble with DATE_FORMAT and STR_TO_TIME with AM/PM - mysql

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

Related

Adding one hour on date time, with AM PM in count

I want to get the data between 2 dates, I have a procedure that takes fromDate as input, for example like if I give this date for it 2022/02/02 9:10:36 PM, and I want to add 3 hours to the ToDate, so it should be like that: 2022/02/02 12:10:36 AM
I tried DATE_ADD('2022/02/02 9:10:36 PM', INTERVAL +3 Hour) but it didn't work it gave it PM and it should be AM since its 12 AM after adding 3 hours to 9 pm.
SUMMARY: I want to get data with 3 hour range in procedure 2022/02/02 9:10:36 ((PM)) to 2022/02/02 12:10:36 ((AM)), the procedure take the from date and i want to add three hours with AM PM in count
First you have to convert the string date to an actual datetime type and then add the hours. Use str_to_date() which allows you to tell the conversion process what the string looks like so it can complete the conversion correctly.
Here is a simple demo
SELECT STR_TO_DATE('2022/02/02 9:10:36 PM', '%Y/%m/%d %h:%i:%s %p') original,
DATE_ADD( STR_TO_DATE('2022/02/02 9:10:36 PM', '%Y/%m/%d %h:%i:%s %p') , INTERVAL +3 Hour) new_dt;
RESULT
original new_dt
2022-02-02 21:10:36 2022-02-03 00:10:36
It is best to store dates and times in the appropriate data type in the first place so functions work correctly automatically. If your location requires a specific presentation of the date, do that conversion in the presentaion layer not the storage layer

MySql convert start_date (VARCHAR) column datatype to start_date (DATETIME) without losing data

I have a start_date(VARCHAR) column which have the data stored in "11/06/15 06:00:00 AM" format.
I'm having issue with Jquery DataTable, it is not sorting this column properly.
I want to convert start_date(VARCHAR) column to start_date(DATETIME) format.
I tried with couple alter commands it replaces date to 0000-00-00 and not converting it properly.
UPDATE videos SET `temp_date` = STR_TO_DATE(`start_date`, '%e/%c/%Y %h:%i:%s.%f %p')
Please suggest me sql query to fix the issue without losing data.
The format of '11/06/15 06:00:00 AM' is '%m/%d/%y %h:%i:%s %p':
SELECT STR_TO_DATE('11/06/15 06:00:00 AM', '%m/%d/%y %h:%i:%s %p')
Result:
2015-06-11 06:00:00
Here you can find all the specifiers that you can use with STR_TO_DATE().

How to compare current time with 26/09/2018 04:10 AM

how to compare time with current time but with AM pm format also compare in mysql select query.
I added Database image where stored time in this formate and i compare to current time but i didn't get success.
You need to convert expectedDeliveryDateTime to a date so you can compare it with NOW(). To do that you need to use STR_TO_DATE with the format '%d/%m/%Y %h:%i %p' which matches the data in your image. So try:
SELECT *
FROM yourtable
WHERE STR_TO_DATE(expectedDeliveryDateTime, '%d/%m/%Y %h:%i %p') >= NOW()
I have assumed you are looking for dates in the future, if you want dates in the past just change >= NOW() to <= NOW().
use now()and format date using date_formatas your column is varchar so you have to convert it date by using STR_TO_DATE function
select * from t where
STR_TO_DATE(expectedDeliveryDateTime, '%d/%m/%Y %h:%i %p') >=date_format(now(), '%d/%m/%Y %h:%i %p')
Please, check my time zone. Are we the same?
SELECT UTC_TIMESTAMP() from dual;
If the same, then you compare is correct.

Unable to get TIME_FORMAT to show 24-hour time

Using mySQL 5.6.
I am storing time in my database like this:
15:00:00
(So that's 3:00 pm. This is what is stored in the database.)
When I want to select data based on time, I do something like this:
#x = "03:00 pm"; (This is what is being passed in from my time widget.)
SELECT * FROM mytable
WHERE start = TIME_FORMAT(#x, '%H:%i:%s');
But this returns 03:00:00, so no match.
(Remember 15:00:00 is in the database, so I need TIME_FORMAT to change 3:00 pm to 15:00:00, not 03:00:00.)
I have tried all of the following:
SELECT TIME_FORMAT('03:00 pm', '%H:%i:%s')
SELECT TIME_FORMAT('03:00 pm', '%h:%i:%s')
SELECT TIME_FORMAT('03:00 pm', '%T')
And NONE of them give me the 15:00:00 that I need.
Here is what the manual says:
This is used like the DATE_FORMAT() function, but the format string may
contain format specifiers only for hours, minutes, seconds, and microseconds.
Other specifiers produce a NULL value or 0.
And here are a number of sites showing examples and the tables that show what formatting symbols will work with the TIME_FORMAT function:
https://www.techonthenet.com/mysql/functions/time_format.php
http://www.java2s.com/Tutorial/MySQL/0280__Date-Time-Functions/TIMEFORMATtimeformat.htm
http://www.w3resource.com/mysql/date-and-time-functions/mysql-time_format-function.php
http://www.mysqltutorial.org/mysql-time/
So, everything here is telling me that what I'm doing should work.
What am I not getting here?
You can use STR_TO_DATE():
select str_to_date('03:00 pm','%h:%i %p')
The following:
SELECT TIME_FORMAT('03:00 pm', '%H:%i:%s')
SELECT TIME_FORMAT('03:00 pm', '%H:%i:%S')
SELECT TIME_FORMAT('03:00 pm', '%T')
are correct to storing your database like your wanting.
maybe you should try to change the 'date' parameter.

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