I need to find all dates that are within two dates. This would normally be done with
BETWEEN
, but I have a StartDate and en EndDate column. This means that the date actually stretches over some days.
So a record could have values like this:
id, status, startDate, endDate
How can I find all rows, that are within those dates, provided I give the query a two dates.
To get records where the complete record period lies in the requested period:
SELECT *
FROM yourtable
WHERE startDate >= smallestDate AND
endDate <= largestDate;
To get records where the record period intersects the requested period:
SELECT *
FROM yourtable
WHERE startDate <= largestDate AND
endDate >= smallestDate;
And, for completeness, to get the records where the start of the records period lies in the requested period, but you don't care about the end of the record period:
SELECT *
FROM yourtable
WHERE startDate BETWEEN smallestDate AND largestDate
And, vice versa, for the end date of the record period:
SELECT *
FROM yourtable
WHERE endDate BETWEEN smallestDate AND largestDate
(All examples assume startDate is always smaller than the endDate, of course.)
Related
In mysql database I have table like this:
StartDate EndDate Price
01.01.2017 01.06.2017 100
02.06.2017 01.12.2017 150
What is the best solution to get price between some days. For example if I send dates 25.05.2017 as start date and 05.06.2017 as end date I need to get sum of price between these dates.
Try this
select sum(Price) as total_price from table_name
where StartDate >= 'given_startdate' and EndDate <= 'given_enddate'
To find overlapping periods use following logic:
where start_1 <= end_2 and end_1 >= start_2
in your case:
where startdate <= date '2017-06-05' and enddate >= date '2017-05-25'
Edit:
Seems like you want to expand the date range to one row per day. Join to a calendar table (if you don't have one, yer, create it, you will used it in many places):
select sum(price)
from mytable join calendar
on calendardate between startdate and enddate
In my database, I have a column with a check-in date and a column with a check-out date. I need to select every row that has a check-in date <= 7/30/2017 and a check-out date that is >= 7/30/2017.
This is the code I have now:
SELECT *
FROM `v_reservation_records`
WHERE cast(checkin as date) <= '7/30/2017'
AND cast(checkout as date) >= '7/30/2017'
Here is an example date from the DB:
2018-09-18
When I run this query, I do not get any results, but I know that I have a check-in date equal to 7/30/2017. What am I missing? Or is there an easier way to accomplish my goal?
Assuming that you are casting valid values for date
You should convert also the literal the date properly
SELECT *
FROM `v_reservation_records`
WHERE cast(checkin as date) <= str_to_date('7/30/2017' , '%d/%m/%Y')
AND cast(checkout as date) >= str_to_date('7/30/2017' , '%d/%m/%Y')
and you can also use between
SELECT *
FROM `v_reservation_records`
WHERE str_to_date('7/30/2017','%d/%m/%Y')
between cast(checkin as date) AND cast(checkout as date)
Try like this
SELECT *
FROM `v_reservation_records`
WHERE DATE_FORMAT(checkin, '%m/%d/%Y') between '7/30/2017'
AND '7/30/2017'
Hi I have a MySQL database which on I am setting up a table for a Study Calendar, fields are as follows:
SELECT
studycalendarpk,
studytopic,
`module`,
startdate,
enddate,
syllabusoutline
FROM studycalendar
What I am trying to do is to create a query so that for a dashboard php page it has a query that dispays the current weeks study programme. Can someone please tell me how to setup query to filter it so that it is selected if the current date is between the startdate and enddate, thank you
You have a startdate and an enddate for each row in your table, and if I understand your requirement correctly, you want to display all rows that meet these criteria.
WHERE enddate >= start of week
AND startdate < start of next week
You already have startdate and enddate in your table. This answer assumes that each row's enddate is constrained to be greater than or equal to the starttdate. If it isn't you'll get strange results.
You need a MySQL expression to compute the first day of the present week. Here's how you do that.
FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -1, 7))
This expression yields the Sunday immediately preceding CURDATE(). If your weeks are considered to start on Monday, use this instead (notice the -2).
FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -2, 7))
These are very useful expressions because they yield actual dates. Those dates can then be manipulated by date arithmetic such as somedate + INTERVAL 7 DAY which conveniently gives you the date a week later. This sort of arithmetic even works for the last week, and the first week, of a calendar year.
Putting it all together, here's what you do to select the records you want.
WHERE enddate >= FROM_DAYS(TO_DAYS(CURDATE())-MOD(TO_DAYS(CURDATE())-1,7))
AND startdate < FROM_DAYS(TO_DAYS(CURDATE())-MOD(TO_DAYS(CURDATE())-1,7))
+ INTERVAL 7 DAY
This will get the records from your table relevant to the current week.
SELECT *
FROM studycalendar
where curdate() between startdate and enddate
Can you try it? We can gett week no of use by this week() method
SELECT
`studycalendarpk`,
`studytopic`,
`module`,
`startdate`,
`enddate`,
CAST(week(now()) AS UNSIGNED)
syllabusoutline
FROM studycalendar WHERE CAST(week(now()) AS UNSIGNED) between CAST(week('2014-09-01') AS UNSIGNED) and CAST(week('2014-09-07') AS UNSIGNED)
I put together a query for determining whether the time difference between the current date and a record from a database is exactly a month or more apart. I am comparing now() to a created_at column, which is a timestamp.
EX:
6-12-2014,
7-12-2014
AND
5-12-2014,
7-12-2014
Should be considered to be a desirable results.
SELECT count(*) FROM `subscriptions` WHERE
DATE_ADD(CAST(created_at as DATE),INTERVAL TIMESTAMPDIFF(MONTH, created_at, now()) MONTH) = CAST(now() as DATE);
However the query appears to not return all desired results. It returns 2-28-2014 and 7-28-2014, however it does not pull up 6-28-2014. Is there a better way of doing this than the solution I came up with?
Are you looking to count dates that are on the same day of the month as the current date? If so, try the DAYOFMONTH function:
SELECT COUNT(*)
FROM subscriptions
WHERE DAYOFMONTH(created_at) = DAYOFMONTH(NOW())
Is it possible to do sql query on what day is today and get the row of today for date column?
So let say today is july 25th, i have database table sales, column name date, date = sales transaction date in timestamp.
i need all row that the sales date is same with current date and also is it possible set value as gmt+5?
This will get you all the rows for today's date:
SELECT * FROM sales
WHERE DATE(NOW()) = DATE(DATE_ADD(sales_transaction, INTERVAL 5 HOUR))
ORDER BY sales_transaction
As for GMT +5, Do you mean all rows with a sales date +5 hours or today +5 hours?
EDIT: Updated to add 5 hours to sales date. For a column called date, I would use the back-ticks to indicate it's a column name. e.g. SELECT `date`` FROM sales
I can't figure out how to work the back-ticks on the date field. But you should get the idea. Wrap your column names with `
Give some time and Check out Date Time Function of mySQL
SELECT * FROM tablename WHERE salesdate = '1998-1-1';
If your date is stored in GMT timezone
Select *
FROM tablename
WHERE DATE_ADD(NOW(), INTERVAL 5 HOUR) = salesdate
SELECT * FROM `sales` WHERE DAYOFMONTH(FROM_UNIXTIME(date)) = DAYOFMONTH(DATE(now()))
seems working.
Thank you for all of your replies.