Google sheets - How to query a custom date range? - google-sheets-query

Trying to work out how to query a custom date range within google sheets - I get it working with a standard date range (YYYY-MM-DD) but with the below being a custom range I don't know how to query custom dates & times
Ideally, I'm trying to query all the data between the start date and 3 hours of the start date
I tried to amend a working query with how the custom DateTime would look but it errors out
=query(B6:B9, "select B where B => date '"&TEXT(B3,"yyyy-mm-ddThh:mm:ss.mmmZ")&"' ",0)
Start Date 2019-04-01T09:32:07.148Z
Dates:
2019-04-01T10:35:01.152Z
2019-05-01T09:42:27.200Z
2019-04-01T12:32:27.250Z
2019-07-11T13:32:07.148Z

There is rather a lot wrong with your attempt, including:
=> should be >=
date should be datetime
for a range (eg 3 hours) you need both an upper and a lower limit
when processing dates and times strings are not suitable inputs
Please convert your text strings (eg by moving what is in B3:B9 to say F6:F9) and instead in B3 (copied down to B6:B9):
=value(substitute(left(F3,23),"T"," "))
then try:
=query(B6:B12,"select B where B >= datetime '"&TEXT(B3-1/8,"yyyy-mm-dd hh:mm:ss.sss")&"' AND B <= datetime '"&TEXT(B3+1/8,"yyyy-mm-dd hh:mm:ss.sss")&"' ",0)

Related

Google Sheets query that can update based on date range in two separate columns (compared to TODAY())

Data Tables
I have two data tables similar to the ones above (see link to Data Tables), where the "Raw Data Table" is an ongoing counter per week of sales calls per person. In the table below, I'd like to create an automated formula that can recognize the date ranges in Week Start (Col1) and Week End (Col2) and give the appropriate this week/last week values as this table is updated throughout the year. I had tried the following formula for Carly (Col3), but only got the column header as the output:
=QUERY(A1:D54,"select C where A >= date '"&TEXT(TODAY(),"yyyy-mm-dd")&"' and B <= date '"&TEXT(TODAY(),"yyyy-mm-dd")&"' ", 1).
Any help here would be great! Thank you so much in advance!

MSAccess: Return query for specific date from datetime value

I am trying to run a query from a linked table in MS SQL Server that has a datetime field. I'm trying to run a simple query to search for records for a specific date (#03/24/2018#), but since the datetime field has the time as well, I am not getting any records unless I specify the time range using BETWEEN with the time (Between #03/24/2018 00:00:00 AM# And #03/24/2018 11:59:59 PM#).
Original query which does not return desired output:
SELECT *
WHERE MyDateTimeField) = #3/24/2018#;
Query
SELECT *
WHERE MyDateTimeField) Between #3/24/2018 00:00:00 AM# And #3/24/2018 23:59:59#);
Is there a workaround to this as to not have to use a BETWEEN operator with the time?
To avoid time portion, check forMyDateTimeFieldequal/greater than searched day and less than next day:
SELECT *
FROM MyTable
WHERE
MyDateTimeField >= #3/24/2018#
AND
MyDateTimeField < DateAdd("d",1,#3/24/2018#);
In opposite of convertingMyDateTimeFieldto date, this does not prevent index usage and handlesNullValues onMyDateTimeField.

Error when runing query from form between two date from a form

I have a form called FirstInLastOut which looks as the image below.
Based on Name or badge number I want to search between two dates.
I am using the following criteria on the query:
>=[Forms]![FirstInLastOut]![StartDateEntry] And <=[Forms]![FirstInLastOut]![EndDateEntry]
This is given me results that include other months as well. Please see the query report below.
So as you can see in the image the numbers of the dates are falling with the the parameter but getting other months as well.
How can I make it so it will only select the dates between the date ranges?
SELECT FistClockInRaw.Badgenumber, FistClockInRaw.name, FistClockInRaw.lastname, FistClockInRaw.MinOfCHECKTIME, FLastClockOutRaw.MaxOfCHECKTIME, [MaxOfCHECKTIME]-[MinOfCHECKTIME] AS TotalHours, FLastClockOutRaw.QDate, FistClockInRaw.MinOfQTime, FLastClockOutRaw.MaxOfQTime, RawCompleteQuery.CHECKTIME
FROM RawCompleteQuery, FLastClockOutRaw INNER JOIN FistClockInRaw ON (FLastClockOutRaw.Badgenumber = FistClockInRaw.Badgenumber) AND (FLastClockOutRaw.name = FistClockInRaw.name) AND (FLastClockOutRaw.QDate = FistClockInRaw.QDate)
WHERE (((FistClockInRaw.name)=[Forms]![FirstInLastOut]![FirstNameEntry]) AND ((RawCompleteQuery.CHECKTIME)>=[Forms]![FirstInLastOut]![StartDateEntry] And (RawCompleteQuery.CHECKTIME)<=[Forms]![FirstInLastOut]![EndDateEntry]));
is the Query
I assume that the forms fields StartDateEntry and EndDateEntry are bound to fields of type date.
I also assume that you are only interested to compare the date part of those form fields.
So try this condition instead to assure correct date interpreting:
WHERE FistClockInRaw.name=[Forms]![FirstInLastOut]![FirstNameEntry]
AND RawCompleteQuery.CHECKTIME >= Format([Forms]![FirstInLastOut]![StartDateEntry], "\#yyyy-mm-dd\#")
AND RawCompleteQuery.CHECKTIME <= Format([Forms]![FirstInLastOut]![EndDateEntry], "\#yyyy-mm-dd\#")
A remark:
Be aware that every date field/variable always contains a time part too!
So your current logic comparing EndDateEntry with <= can cause trouble, because you would only get results of the end date having time values of 00:00:00 in the field CHECKTIME.
If any record of CHECKTIME contains the requested end date and a time part bigger then 00:00:00, it is not in the result.
To avoid that, you should use < and add one day:
And RawCompleteQuery.CHECKTIME < Format([Forms]![FirstInLastOut]![EndDateEntry] + 1, "\#yyyy-mm-dd\#")

How to search data for a date value after than the specified one?

I have a basic SQL knowledge and I am trying to retrieve data from my database based on the date a user chooses. So, if the user selects 2018-03-01 I want to display the data for the date value 2018-03-02.
I can search data for a specified date in the following way:
select * from FLIGHTS where DEPARTURE_DATE = '2018-03-01';
Now I want to search the data for the date next to the specified so for '2018-03-02' in this case. I cant directly search for 2nd March because I don't know what date the user will choose
So is there any way to query data for the date next to the one specified?
I have already tried looking up everywhere but couldn't find anything that makes sense to me.
Thx
I think this works well. By the documentation for date/time functions, you should be able to do something like,
select * from FLIGHTS where DEPARTURE_DATE = '2018-03-01' + INTERVAL 1 DAY
What sort of element are you using for the user to be able to get their date and what is your codebase? We have mysql for the DB but we need to know if your codebase is c#, PHP etc.
Most languages generally have a function to allow you to add days to a datetime, if for instance you used C# and had a datetime selector posting its value you can do:
DateTime dateField = postedDateTime.AddDays(1);
from there you can easily escape the value and push it through your db call.
or PHP:
$dateField = date('Y-m-d', strtotime($postedDateTime. ' + 1 days'));
If you would like a date range (date selected and the next day) then you would adjust your php code to have:
$dateFrom = $postedDateTime;
$dateTo = date('Y-m-d', strtotime($postedDateTime. ' + 2 days'));
$dateTo = date('Y-m-d', strtotime($dateTo. ' - 1 seconds'));
//DB Query
$query = "select * from FLIGHT where DEPARTURE_DATE >= :dateFrom AND DEPARTURE_DATE <= :dateTo";
The above assuming you're using pdo, but you can see how to adjust for your situation. I am also assuming the datetime is based on midnight for each day so you may need to adjust your dateTo to take from 1 second before midnight the day after just so you can get the entire days data
You can make use of interval as day as mentioned in the answer. Along with day you can also have day_minute, day_hour,day_second but be sure when you are adding decimals on these.

Filtering the dates range based on the days

I have a table with columns as
Id (1)
Date holding values as (2013-12-12 00:00:00)
Start time (00:00:00)
end time (01:00:00)
value
The user will give the date range and will specify day like sunday ,monday etc .
How can I use a sql query to filter the dates and find the proper days between that for the specified days as well.
One way would be to use BETWEEN ... AND ... and DAYOFWEEK():
SELECT *
FROM my_table
WHERE Date BETWEEN ? AND ?
AND DAYOFWEEK(Date) = ? -- 1=Sunday ... 7=Saturday