0

Here I have some records, I want to search for records from 8:30 AM to 9:30 AM.

Screenshot of phpmyadmin

I tried these query but not working:

select * from table_name where '8:30 AM' BETWEEN start_time and end_time OR '9:30 AM' BETWEEN start_time and end_time

select *
from time_test
where STR_TO_DATE(start_time, '%h:%i %p') BETWEEN STR_TO_DATE('08:30 AM', '%h:%i %p') AND STR_TO_DATE('09:30 AM', '%h:%i %p')
    OR STR_TO_DATE(end_time, '%h:%i %p') BETWEEN STR_TO_DATE('08:30 AM', '%h:%i %p') AND STR_TO_DATE('09:30 AM', '%h:%i %p')

Here is table sql :

DROP TABLE IF EXISTS `time_test`;
CREATE TABLE IF NOT EXISTS `time_test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `start_time` varchar(20) NOT NULL,
  `end_time` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;

INSERT INTO `time_test` (`id`, `name`, `start_time`, `end_time`) VALUES
(1, 'R1', '8:00 AM', '9:00 AM'),
(2, 'R2', '9:00 AM', '10:00 AM'),
(3, 'R1', '8:00', '9:00'),
(4, 'R2', '9:00', '10:00'),
(5, 'R2', '1:00 PM ', '2:00 PM'),
(6, 'R2', '13:00 ', '14:00'),
(7, 'R1', '8:00 PM', '9:00 PM'),
(8, 'R1', '8:30 AM', '9:30 AM');
Deepak
  • 47
  • 1
  • 6

3 Answers3

1

You could use the DATE() function. In my example using DATETIME. Can try with your database records.

SELECT `yourdatabase`
  FROM `yourtable`
 WHERE DATETIME(`yourcolumn`) = '2019-06-17 08:30'

However, for better performance you could use..

  WHERE `yourcolumn` 
BETWEEN '2019-06-17 08:30:00'
    AND '2019-06-17 09:30:00'

You can check HERE for details or read THIS.

Ivan Bahri
  • 89
  • 3
  • 14
  • I tried DATETIME function in PHP MySQL it's not working. And I tried this query also "SELECT * FROM `time_test` WHERE `start_time` BETWEEN '2019-06-17 08:30:00' AND '2019-06-17 09:30:00' " it's also not working. – Deepak Sep 20 '19 at 04:24
  • In my code, using `DATETIME`, in your case you using `TIME`. Please, read my detail answer. – Ivan Bahri Sep 20 '19 at 07:11
  • Hi, thanks for help. I tried this query "select * from time_test where TIME(start_time) BETWEEN TIME('07:00 AM') AND TIME('10:00 AM') AND TIME(end_time) BETWEEN TIME('07:00 AM') AND TIME('10:00 AM')" but its not working only with AM PM. – Deepak Sep 25 '19 at 15:57
0

You can convert to minutes and add where conditions: View example :

SELECT *
FROM my_table 
WHERE 
    ( 
        HOUR(start_time ) * 60 + MINUTE(start_time) >= HOUR('8:30 AM' ) * 60 + MINUTE('8:30 AM')
        AND 
        HOUR(start_time ) * 60 + MINUTE(start_time) <= HOUR('9:30 AM' ) * 60 + MINUTE('9:30 AM')
    )
    OR 
    ( 
        HOUR(end_time ) * 60 + MINUTE(end_time) >= HOUR('8:30 AM' ) * 60 + MINUTE('8:30 AM')
        AND 
        HOUR(end_time) * 60 + MINUTE(end_time) <= HOUR('9:30 AM' ) * 60 + MINUTE('9:30 AM')
    )
Au Nguyen
  • 655
  • 4
  • 12
  • Hi AU Nguyen, thank you so much for the help, I tried this above query but should not show these records which have start_time " 8:00 PM" and end_time "9:00 PM". It's fetching these records also. – Deepak Sep 20 '19 at 04:42
  • Because your string query used "Or", so I used "OR" in my query. You can replace "OR" with "AND" to get records with start_time and end_time between '8:30 AM' - '9:30 AM' – Au Nguyen Sep 20 '19 at 05:11
  • I tried this query but still not working in AM PM Cases. – Deepak Sep 25 '19 at 15:47
  • I tried this query "SELECT * FROM time_test WHERE ( HOUR(start_time ) * 60 + MINUTE(start_time) >= HOUR('7:00 AM' ) * 60 + MINUTE('7:00 AM') AND HOUR(start_time ) * 60 + MINUTE(start_time) <= HOUR('10:30 AM' ) * 60 + MINUTE('10:30 AM') ) AND ( HOUR(end_time ) * 60 + MINUTE(end_time) >= HOUR('7:00 AM' ) * 60 + MINUTE('7:00 AM') AND HOUR(end_time) * 60 + MINUTE(end_time) <= HOUR('10:30 AM' ) * 60 + MINUTE('10:30 AM') )" But it's also returning PM records. – Deepak Sep 25 '19 at 16:05
  • @Deepak, you can use STR_TO_DATE to convert 24h before get hour. SELECT HOUR(STR_TO_DATE('08:35 PM', '%l:%i %p')) – Au Nguyen Sep 26 '19 at 02:10
  • Hi @Au Naguyen thanks, But the main problem I am facing convert 12 hour format time to 24 hour format str_to_date function is returning null means that this function is not working. – Deepak Sep 26 '19 at 03:06
  • I have google about time conversion and found this https://stackoverflow.com/questions/986058/mysql-12-hr-to-24-hr-time-conversion and tried "select time_format(str_to_date('01:56:39 PM','%r'),'%T')" but this is also not working I think this is not working because of str_to_date() function returning null. – Deepak Sep 26 '19 at 03:32
0

since you're not storing the data as DATETIME format, to select a records ranged limited to only within 8:30 to 9:30, you could use STR_TO_DATE :

select *
from table_name
where (STR_TO_DATE(start_time, '%h:%i %p') BETWEEN STR_TO_DATE('08:30 AM', '%h:%i %p') AND STR_TO_DATE('09:30 AM', '%h:%i %p'))
    AND (STR_TO_DATE(end_time, '%h:%i %p') BETWEEN STR_TO_DATE('08:30 AM', '%h:%i %p') AND STR_TO_DATE('09:30 AM', '%h:%i %p'))

To also select a records ranged outside the 8:30 and 9:30 range :

select *
from table_name
where (STR_TO_DATE(start_time, '%h:%i %p') BETWEEN STR_TO_DATE('08:30 AM', '%h:%i %p') AND STR_TO_DATE('09:30 AM', '%h:%i %p'))
    OR (STR_TO_DATE(end_time, '%h:%i %p') BETWEEN STR_TO_DATE('08:30 AM', '%h:%i %p') AND STR_TO_DATE('09:30 AM', '%h:%i %p'))
Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26
  • Hi, thanks for help but above query not working properly with AM PM records result. – Deepak Sep 21 '19 at 10:36
  • Hi, I've updated my codes, using `STR_TO_DATE` instead – Hasta Dhana Sep 23 '19 at 06:47
  • thanks for help, But str_to_date returning null and query is not working. – Deepak Sep 25 '19 at 15:50
  • I've updated the query, and I also have tested using your table sql and it is work (returns rows). What is returning null? – Hasta Dhana Sep 26 '19 at 03:59
  • Hmm, if it's working for you then why not working in my system. I tried this query in PHPMyAdmin but not working. what could be the reason? – Deepak Sep 26 '19 at 06:35
  • [here](https://prntscr.com/pb6xy5) is the result of the 1st query and [here](https://prntscr.com/pb6yq0) is the result of the 2nd query, are you sure you didn't miss anything? – Hasta Dhana Sep 26 '19 at 09:43
  • Hi big thanks for the help, I have found a solution, why this query was not working in my system. to understand problem go to this link: https://stackoverflow.com/questions/41318941/mysql-str-to-date-function-not-work-for-time So for this I will have to change SQL mode in MySQL – Deepak Sep 27 '19 at 03:11