Include selected date also in date range - mysql

I am queering a data in a date range in MySQL. When I select the date range
BETWEEN '2013-01-19 00:00:00' AND '2013-01-21 00:00:00' the data is shown only 19 and 20 date but not the 21. When I write 22 over 21 then data shown from 19-21 not include 22. Now how should I write a query to include the selected date also in date range.
Eg: BETWEEN '2013-01-19 00:00:00' AND '2013-01-21 00:00:00'
Show the data from 19,20,21 Jan 2013.
Please help me

Actually it includes 2013-01-21 if and only if the recorded date and time is 2013-01-21 00:00:00. the exclusive date starts on 2013-01-21 00:00:01 and up
so to solve your problem, use 23:59:59 to include the whole time of the selected day.
BETWEEN '2013-01-19 00:00:00' AND '2013-01-21 23:59:59'

If you want to select data based only on the date, use the function DATE() to extract the date part of the data. I.e.
DATE(fieldname) BETWEEN '2013-01-19' AND '2013-01-21'

Related

mysql select on datetime objects within range

I have a table 'appointments' that contains, among other things, two datetime fields labeled 'start' and 'end'. I also have date in local time that is converted as a range of a full day into UTC (which is what the SQL table stores the datetimes as). I need to select all the (business) times between 00:00:00 and 08:00:00 UTC that also fall in the range of my local time conversion.
An example, A user in PST (pacific standard time) picks December 1st, 2018. The dates between December 1st at 00:00:00 and December 2nd 00:00:00 are converted to UTC which would be December 1st 08:00:00 to December 2nd 08:00:00. I need to select all appointments between 00:00:00 and 8:00:00 any given day in the previous range (dec 1 - dec 2).
All of my datetimes/queries are in the form 'yyyy-MM-dd HH:mm:ss'.
I know that I can select all of the times between two times rather simply like so:
SELECT start, end
FROM appointment
WHERE start>='2018-12-01 00:00:00'
AND end<='2018-12-02 08:00:00'
But I'm unsure as to how to trim these down to only between business hours.
I'm looking for something like
SELECT start, end
FROM appointment
WHERE (start>='2018-12-01 00:00:00'
AND end<='2018-12-02 08:00:00')
AND (start.substring(11, start.end) >= '00:00:00'
AND end.substring(11, end.end) <= '08:00:00')
Where a call like start.substring(11, start.end) would return the time in 'HH:mm:ss' format
Try using the TIME function in MySQL.
SELECT start, end
FROM appointment
WHERE TIME(start) >= '00:00:00'
AND TIME(end) <= '08:00:00' AND ... //other conditions

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);

mysql and date comparison with date function and without

I notice a strange behavior for me when using date() and without:
when I use
SELECT * FROM `mytable` WHERE date(date_add) >= '2017-08-01' and date(date_add) <= '2017-08-31'
I get all dates records within the given date range but if do:
SELECT * FROM `mytable` WHERE date_add >= '2017-08-01' and date_add <= '2017-08-31'
I don't get the rows from the last day 31, why? (the date_add field is datetime type)
EDIT:
How should I code date range correctly? Because what I understand so far is that if I don't use full time like YYYY-MM-DD HH-MM-SS I should always compare with DATE() to avoid missing results from the last day.
That's because date_add is a DATETIME field. If the time for the date 2017-08-31 is something like 08:15:00 or 13:21:00 in your table, your date is "bigger" than just 2017-08-31 00:00:00. Your comparison would just return data for the 2017-08-31 having the time 00:00:00.
Because without the date() transformation your comparison imply the values '2017-08-01 00:00:00' and '2017-08-01 00:00:00'

Select Range of Items by Year and Month

I'm attempting to create a select statement which gets items by year and month.
This is what I have so far:
SELECT * FROM sales WHERE YEAR(Date) = 2013 AND MONTH(?) = 'June'
I can't merely select date ranges because different months have different number days. It would be ideal to select months by their number (ie, January being 1) or a similar approach.
How is this worked out in a mysql statement?
The fields are datetime fields such as 2012-12-01 00:00:00
Have a look at the performance and write your condition as
SELECT * FROM sales
WHERE
Date >= '2013-06-01 00:00:00'
AND
Date < '2013-07-01 00:00:00'
for the example month: June of 2013
so MySQL can use an index on the column date. You will get exactly all rows with a date in the June of 2013, even those in the first second, but not those in the first second of the July of 2013.
You see, that you don't need to know the number of days of the particular month, because you will ever use the first of both months.
You could use a bit of date calculation too:
SELECT * FROM sales
WHERE
Date >= '2013-06-01 00:00:00'
AND
Date < '2013-06-01 00:00:00' + INTERVAL 1 MONTH
so you need only to know the start and the length of the interval.
Note
The most important part of my answer is to use the column Date without using a function on this column, so MySQL can use an index.

How to check if date is in range regardless of the year

I'm wondering what would be the easiest way in MySQL to check if given date is in range regardless of the year.
In database table I have two DATE fields: start and finish stored in YYYY-mm-dd
if start = 2013-11-01 and finish = 2014-03-01 anything between 1st of November and 1st of March of any year should be accepted.
Valid dates:
2020-01-01 1980-02-28
Invalid dates:
2013-10-30 1968-07-30
There are almost certainly cleaner ways of doing it, however this should work:
((DAYOFYEAR(finish_date) > DAYOFYEAR(start_date)
AND (DAYOFYEAR(#date) >= DAYOFYEAR(start_date)
AND DAYOFYEAR(#date) <= DAYOFYEAR(finish_date)))
OR (DAYOFYEAR(finish_date) <= DAYOFYEAR(start_date)
AND (DAYOFYEAR(#date) >= DAYOFYEAR(start_date)
OR DAYOFYEAR(#date) <= DAYOFYEAR(finish_date))))
For a start date in Oct 2012 and end date in Nov 2020 this will return all dates in the Oct-Nov range. If in fact would want it to return all Dates when the range is greater than a year (and hence covers all dates of the year) you could add:
OR DATEDIFF(Day, start_date, finish_date) > 356
before the final bracket.
use DAYOFYEAR:
When the Start Date is earlier in the year than the Finished Date:
the tested Date should lye between Start Date and Finish Date (or on Start or Finish)
When the Finished Date is earlier in the year than the Start Date:
the tested Date should lye outside the Start Date and Finish Date (or on Start or Finish)
You can use some date extract function and then check your condition..
for example.
SELECT EXTRACT(MONTH FROM TIMESTAMP '2013-11-01 20:38:40');
this will give ouput start month as 11
SELECT EXTRACT(MONTH FROM TIMESTAMP '2014-03-01 20:38:40');
this will give ouput end month as 3
now you can check the condition from above two result..
SELECT * FROM tableWithDates t WHERE month(t.start) >= 11 AND month(t.finish) < 3
if you want the first of march it will go like this:
SELECT * FROM tableWithDates t WHERE month(t.start) >= 11 AND (month(t.finish) < 3 OR month(finish) <= 3 AND day(finish)<=1)
Depending on the size of the data you will run this at. You can get into performance problems, as MySQL can't use indexes of calculated columns.
If you run into this i suggest spitting the month AND/OR day into separate columns.
Edit:
Given an one parameter input as '2008-02-29'
SELECT * FROM tableWithDates t
WHERE
month(t.start) >= month('2008-02-29') AND day(t.start) >= day('2008-02-29')
AND month(t.finish) <= month('2008-02-29') AND day(t.finish) <= day('2008-02-29')