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
Related
Good day,
I am trying to fetch range of data in my database but i cant get any result when i try to query like this.
SELECT * FROM `doc_history` WHERE date BETWEEN '07-13-2020 Mon 10:13:32 am' AND '07-20-2020 Mon 01:24:28 pm'
but if i try only to search specific date. it normally show the data. but when it comes to RANGE SEARCH, it show nothing.
I am trying to convert this date format to a simplified date format "07-20-2020 Mon 01:24:28 pm" to "07-20-2020".
I tried others formula but still no result.
You should valid MySQL timestamp/datetime literals:
SELECT *
FROM doc_history
WHERE STR_TO_DATE(date, '%m-%d-%Y %a %h:%i:%s %p')
BETWEEN '2020-07-20 13:24:28' AND '2020-07-20 13:24:28';
But note that you are currently not really searching for a range, but rather a point in time. So the above query I wrote is equivalent to:
SELECT *
FROM doc_history
WHERE STR_TO_DATE(date, '%m-%d-%Y %a %h:%i:%s %p') = '2020-07-20 13:24:28';
If you want a meaningful range, then the start and end values should be different points in time.
I am rounding time to the nearest hour using the query below:
SELECT post_id, DeliveryDate, TIME_FORMAT(DeliveryTime, '%h:00:00 %p') AS rounded_time FROM `orders` WHERE DeliveryType='delivery' AND DeliveryDate >= CURDATE() - Interval 30 DAY ORDER BY `DeliveryDate`
IT correctly rounds everything but applies 'AM' to all results even though most are in PM in the db.
Here is the original db data.
enter image description here
How do I get this to correctly read the AM/PM when using rounding like I am?
The column's DeliveryTime is VARCHAR, so first you must convert it to TIME with STR_TO_DATE() and then apply the formatting.
If you are using MySql 8.0, this will work:
SELECT TIME_FORMAT(STR_TO_DATE(DeliveryTime, '%h:%i%p'), '%h:00:00 %p') rounded_time
FROM orders
See the demo.
If you use a previous version, you must concatenate a date part to DeliveryTime to convert it to DATETIME:
SELECT TIME_FORMAT(STR_TO_DATE(CONCAT(CURRENT_DATE, ' ', DeliveryTime), '%Y-%m-%d %h:%i%p'), '%h:00:00 %p') rounded_time
FROM orders
See the demo.
Note that this code just strips the minutes from the time. It does not actually round to the nearest hour.
But if all your time values are hh:00 and hh:30 then it will work.
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
I want to select records of 5 days ago with mysql, but the problem is that my date column is varchar and in the following format yyyy-mm-dd 00:00:00 AM/PM.
How can I convert this date format to mysql date and select records from 5 days back?
my date column name is 'date_time'
This is a job for STR_TO_DATE().
Try this.
... WHERE DATE(STR_TO_DATE(date_time,'%Y-%m-%d %h:%i:%s %p')) = CURDATE() - INTERVAL 5 DAY
The '%Y-%m-%d %h:%i:%s %p' DATE_FORMAT() string matches your textual date format.
This will never be fast if you have to search through a large number of rows, because it isn't sargable. It can't exploit an index.
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.