SQL where two time values between two time columns - mysql

With MySQL, I need to find rows where two values are between two columns.
For example, I'm creating a schedule from '15:00' to '18:00', but the query must verify if any row has an period in use. If there is a record from '14:00' to '18:30', I need to return it to validate.
I tried something like, but not works:
select * from availabilities where (('15:00' or '18:00') between start_hour and end_hour)
Any idea?

This is basically a "test if range overlaps another range" question. The following query should work as long as start time is less than end time:
SELECT *
FROM availabilities
WHERE '18:00' > start_hour AND end_hour > '15:00'

You can use TIME() function to extract the time part of the datatime/timestamp column
Eg.
SELECT * FROM Table
WHERE TIME('timeStampCol') BETWEEN '12:00:00' AND '18:00:00'

Related

MySQL - Return Row Where Current Date Between Start and End Date

I have searched everywhere and can't find an answer for this.
The table below shows the class start and end time.
I am looking to return the row where the current time falls in-between the current start and end time.
The start and End time are going to be variable dependent on current time, so can't input specific values.
An exmaple being, Say if the current time is:
2016-02-05 20:15:00
Then it would return the row where Class ID is 6.
You can use any of the below queries
SELECT *
FROM Table
WHERE NOW() BETWEEN Class_Start_Timestamp AND Class_End_Timestamp
OR
SELECT *
FROM Table
WHERE NOW() >= Class_Start_Timestamp AND NOW() <= Class_End_Timestamp
SELECT * FROM TABLE
WHERE Class_Start_Timestamp > GETDATE()
AND Class_End_Timestamp < GETDATE()
If the columns are not datetime:
SELECT * FROM TABLE
WHERE CAST(Class_Start_Timestamp AS datetime) > GETDATE()
AND CAST(Class_End_Timestamp AS datetime) < GETDATE()
You can also use "NOW()" (mysql) and "BETWEEN", but a quick google should have yielded many options...

mysql filter by date and time separately

I have to perform a query on a MySQL database.
I have a table with records, have a column called "date" (the date type), and a column called "time" (type. Integer is stored by multiplying the time of day by 60. eg 8 am is stored as 480).
Unfortunately, the format of this table can not be modified.
My table stores attentions of doctors on call. The doctors on duty working in two shifts: from 8-20, and 20-8.
I need to know the amount of attention for every doctor.
My query must be filtered by date range and shift.
The problem is that, in the case of doctors working at the turn of 20-8, I have to consider a change of day. (sorry for my bad English).
What I have done is this, this would be an example to date of yesterday, and doctors shift 20-8.
SELECT * FROM attentions WHERE (date >= '2015-07-23' and time >=1200) and (date <= '2015-07-24' and time <480)
the query does not work at all.
Supposing the date field is called: 'a_date' with format 'yyyy-mm-ss' and the time field is a number, the query should be:
SELECT * FROM attentions WHERE (date(a_date) >= '2015-07-23' and time >=1200) and (date(a_date) <= '2015-07-24' and time <480)
Can you check using between?
SELECT * FROM attentions WHERE date between '2015-07-23' and '2015-07-24' and time between 1200 and 480
I think you can also use this -
SELECT * FROM ***** where CREATED_DATETIME between '2015-03-12 00:00:00' and '2015-05-11 00:00:00';

MySQL query to search in between two time range between two dates using timestamp data

I have timestamp values in my db. It has values like 2014-11-25 10:30:00.
I need to get all records between two dates and that has time between certain range like between 2014-10-20 to 2014-11-25 and between 9am to 7pm..
I need the query for this...
You can use the following query , I used it in my code for displaying data between two dates.
SELECT * from tablename WHERE columnname BETWEEN '2014-10-20 00:00:00' AND '2014-11-25 23:59:59'
The query includes start time of the particular date to end time of ending particular date.
You edit your query according to your start and end timings.
You can use internal mysql functions for convert datetype.
I think you need DATE() and TIME() functions.
Details you can find here
Thanks for your reply guys. I have found the answer
SELECT * FROM alerts
WHERE DATE BETWEEN '2014-11-16' AND '2014-11-26'
AND TIME(DATE) BETWEEN '09:00' AND '19:00'
Is giving the expected result.. :-)

mySQL query between two dates and two times

I would like to query a mySQL table to pull out data between two dates and two times. I know how to do this for a single "datetime" column using the "between" call but my columns are one "date" column, and one "time" column. All the solution I can find online are for single datetime columns.
My ranges go from "day1" at 15:30 to day1+1day at 15:14
So far I can get the following range (which works):
SELECT time,
close
FROM intraday_values
WHERE date="2005-03-01"
and time between "15:30" and "23:59"
But I obviously need to incorporate 2 dates and two times. I have tried the following but get an error:
SELECT time,
close
FROM intraday_values
between date="2005-03-01"
and time="15:30"
and date="2005-03-02"
and time = "15:14"
Could someone help me formulate the query correctly? Many thanks
Not sure if your date field is indexed. If they are then the "concat" examples others have given may not perform very well.
As an alternative you can use a query of the form:
select *
from foo
where (date > lower_date and date < upper_date) -- technically this clause isn't needed if they are a day apart
or (date = lower_date and time >= lower_time)
or (date = upper_date and time <= upper_time)
It's not pretty but it works and will allow mysql to make use of indexes on the date field if they exist.
So your query would be
SELECT time,
close
FROM intraday_values
where (date > "2005-03-01" and date < "2005-03-02")
or (date = "2005-03-01" and time >= "15:30")
or (date = "2005-03-02" and time <= "15:14")
Use concat to combine these two column and cast it to datetime for the best results
SELECT
time,close
FROM
intraday_values
where
cast(concat(date," ",time) as datetime)
between
cast("2005-03-01 15:30") as datetime
and cast("2005-03-02 15:14") as datetime

Sql - find if date is between two dates

I have a field of FromDate and a field of ToDate.
I am looking for the rows that today is between the "from" and "to"
select * from job
where job.type='manager'
and '2014-01-22' between job.FromDate and job.ToDate
The query does not throw an exception , and it even returns some rows. But it isn't right- the rows it returns do not have the dates I am looking for.
P.S. the date format I am using is the correct one for my DB.
Try this
select * from job
where job.type='manager'
and job.FromDate <= '2014-01-22' and job.ToDate >= '2014-01-22'
Comparing dates is often tricky, especially if the values are stored as datetime and not date. The time components can affect the comparison. Another possibility is that ToDate is NULL for the most recent records. Here is one way to fix this:
select *
from job
where job.type ='manager' and
date('2014-01-22') between date(job.FromDate) and date(coalesce(job.ToDate, '2099-12-31'))
However, the use of the function on the columns can make the query less efficient. Instead, you might try:
select *
from job
where job.type ='manager' and
job.FromDate < '2014-01-23' and
(job.ToDate >= '2014-01-22' or job.ToDate is null);