Retrieve records from table based on two datetime fields - mysql

I have the following data in the table
from_date to_date
2015-05-12 2015-10-20
2015-10-21 2016-02-02
2016-02-03 NULL
Where NULL value in to_date denotes that this record is valid until this time.
The records I want to retrieve is between '2015-10-30' and '2016-08-08'. How do I get the second and third rows based on my search criteria?

I am confused why are you expecting the second row in the result set. Is it something loose range searching (either by from_date or by to_date)?
You can try something like that:
SELECT
*
FROM your_table
WHERE from_date >= startDate
AND IF(to_date IS NULL, TRUE, to_date <= endDate);
Note: Here startDate and endDate are the dates in your given range.
EDIT:
SELECT
*
FROM your_table
WHERE
'2015-10-30' BETWEEN from_date AND COALESCE(to_date,CURDATE())
OR
'2016-08-08' BETWEEN from_date AND COALESCE(to_date,CURDATE())

Finally, I found out what I was looking for.
select * from table where
end_date >= start_search && start_date <= end_search
More detailed answer in the link below. Answered by pacerier
mysql-query-for-certain-date-range

Related

Select only if date in row has not passed yet

I'm selecting rows that have a 'Date' column. The date is formatted like so: 2022-01-08 14:00:00.
So today is: 2022-01-05 11:00, I wish to select every row that has a date beyond today's date.
Is there any way that I can select the rows where the date has not yet passed?
EDIT:
Ergest Basha's comment was just what I was looking for:
Date <= current_timestamp()
You can use:
select * from table_name where date >= now()

MySQL searching timestamp columns by date only

I am building out a query to search a table by a timestamp column value. An example of the format I am passing to the api is 2018-10-10. The user has the ability to select a date range. Often times the date range start date is 2018-10-10 and end date is the same day, 2018-10-10. The below doesn't seem to do the trick. What is the simplest way to accomplish this without having to specify the time? Obviously, I'd like to query for the entire day of 2018-10-10 from start to end of day.
SELECT
count(*)
FROM
contact
WHERE
created_at >= '2018-10-10'
AND created_at <= '2018-10-10';
The problem here is that Timestamp datatype will have HH:MM:SS (time) values also. While comparing a datetime with date, MySQL would automatically assume 00:00:00 as HH:MM:SS for the date value.
So, 2018-10-10 12:23:22 will not match the following condition: created_at <= '2018-10-10'; since it would be treated as: 2018-10-10 12:23:22 <= '2018-10-10 00:00:00, which is false
To handle this, you can add one day to the date (date_to in the filter), and use < operator for range checking.
SELECT
count(*)
FROM
contact
WHERE
created_at >= '2018-10-10'
AND created_at < ('2018-10-10' + INTERVAL 1 DAY);

SQL query to select rows where the year is between two dates

Given a Table with from_date date and a to_date date column, I want to select all sets of data where an year, for example 2007, is the year of one of the dates or both or is in between the two dates.
How can i achieve this using mySQL?
Table:
create Table Articleprice
(`ArtikelpreisNR` int, `ArtikelNR` int, `from_Date` date, `to_date` date, `price` int);
Example Testdata
insert into Articleprice
(`ArtikelPreisNR`, `ArtikelNR`,`from_Date`, `to_Date` ,`price`)
values (1, 1, '2006-7-04', '2008-7-04', 10);
SQL:
This is returning only the ones, when from_date and to_date = 2007
SELECT
MIN(price)
FROM Articleprice
WHERE Articleprice.from_Date >= '2007/01/01' and Articleprice.to_Date <= '2007/12/31';
What do I need to do in order to solve my Case?
First you build complete dates using the year. For 2007, the dates would be 2007-01-01 and 2007-12-31 (assuming that end dates are inclusive).
Next you use overlapping dates query to find all rows that (i) end inside (ii) start inside (iii) contain or (iv) contained within the year 2007. The query is simply:
SELECT * FROM Articleprice
WHERE '2007-12-31' >= from_Date AND to_date > '2007-01-01'
Assuming your dates are actually dates and not strings, you need to use the proper date format in your query: YYYY-MM-DD not YYY/MM/DD:
WHERE Articleprice.from_Date >= '2007-01-01' and Articleprice.to_Date <= '2007-12-31';

statement to check if the datetime is within the two datetime from the column

I have 2 column, start_date and end_date which contains a date and time with "yyyy-MM-dd H:mm:tt" format. At my program, I wanna query with this logic:
If current_date is between start_date and end_date, return True. But the start_date and end_date is not static. Variety of value for each column.
How to make a query statement to check if the current date is between start_date and end_date for every row?
Answer for Alex:
select if(start_date<=now() and now()<=end_date,true,false) from table
You will get 1 (true) or 0 (false).

SELECT MySQL rows where today's date is between two DATE columns

How can I get the rows in a table where today's date is between (inclusive) two DATE columns of that row?
For example, take these two columns of a table:
How could I get the first and second rows on the 10th of April, or the 3rd row on the 25th (inclusive, like I said)?
Any help would be greatly appreciated. Thanks in advance!
You can add a condition as follows
DATE(NOW()) between date1 and date2
You will find a lot of people using between operator, but I prefer using a simple AND operator.
I do that because although the between operator IS inclusive, simple dates (2012-04-10) can be counted as being midnight, and will thus not be inclusive.
So this should work just fine and will always include the boundaries of the date range:
SELECT * FROM table WHERE from_date <= '2012-04-10' AND to_date >= '2012-04-10'
Just use the SQL now() function to compare the date columns like so:
SELECT * from table where now() >= from_date and now() <= to_date
If you have date (not datetime) columns, use CURTIME() or DATE(NOW()), never NOW() as CesarC correct wrote and you can use BETWEEN.
SELECT * FROM table WHERE CURTIME() BETWEEN from_date AND to_date