简体   繁体   中英

Sql query with time and date

This is a part of my sql query, I am very unsure when I for example do a query from date 11.05.2014 to 15.05.2014 with the time from 11.00.00 to 21.00.00.

The problem is that the results of for example 12.05.2014 23.00.00 won't show because it is not between the time from and to. I understand that I can only then search on date without time, but I also want the time for more specific results. Is it a solution for this problem ?

This sql query is just a part of the larger sql query. Thanks.

WHERE routines.date between '".$fromDate."' AND '".$toDate."'

AND routines.time between '".$fromTime."' AND '".$toTime."'

To correct:

The problem with the sql query is that it will show records for 13.05.2014, 13.05.2014, etc. But the time for those will only be between 11.00.00 and 21.00.00, but 24 hours. I only want the sql query to search from Time and To time those specified days..

I don't know reason why you created two columns with date and time instead of one, but for this query you need to combine them. For example, you can create computed column and create an index on it.

Also, you can try this solution, but it will not use indexes if you have them on routines.date and routines.time :

WHERE CAST(CONVERT(varchar(8),routines.date,112)+
      ' '+CONVERT(varchar(8),routines.time,108) as datetime) BETWEEN '".$fromDateTime."' AND '".$toDateTime."'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM