Using Today() function in a query with standard dates in Google Sheets - datetime

I am working with data output from a program and then uploaded into google sheets that looks like the following:
So my timestamp is in the format of 1/2/2020 8:56 (and I totally get that query has to work with yyyy-mm-dd format, which is why we have to do the acrobatics)
I'm using the query function to pull needed data into different tabs and would like to use the Today() or Now() function to only pull the last 12 or 24 hours results and I can't seem to get it to work. (Mind that I'm just learning the query function)
So I have
=query(RawDataUpload!A:I,"select * where A is not null and A >= date '2020-01-02' and B = 'Buying' and H > 0 and H < 50000 Order by D, H desc")
and it works ok, but I have to put in the date each new day and at 8am it's only 8 hours of data instead of 12 (more of a problem at 2am)
I've tried using a few examples and I keep getting a parsing error or some error
=query(RawDataUpload!A:I,"select * where A is not null and A >= date '"&TEXT(TODAY(),"yyyy-mm-dd")&"'",1 and B = 'Buying' and H > 0 and H < 50000 Order by D, H desc")")
=query(RawDataUpload!A:I,"select * where A is not null and A >= date '"&TEXT(TODAY(),"yyyy-mm-dd")&"'" and B = 'Buying' and H > 0 and H < 50000 Order by D, H desc")")
[I see & thrown in but no explanation of why or what it does, and same to the " " instead of ' ' and why the mix mash of using both in the examples of using today() and I've found 0 examples of using the now instead of the date function in my googling.]
So is there a way to limit by date (and possibly time) using the today() or Now()-12 function embedded in the Query function in google sheets?

Try
=query(RawDataUpload!A:I,"select * where A is not null and A >= date '"&TEXT(TODAY(),"yyyy-mm-dd")&"' and B = 'Buying' and H > 0 and H < 50000 Order by D, H desc")
The & is used to join a string to form a formula.
For example, if you want the formula to read:
... and A >= date '2020-01-03' and B =....
But you'd like the date to be today's date, you would use:
...and A >= date '"&TEXT(TODAY(),"yyyy-mm-dd")&"' and B =...
The " is used to exit the query string and add the Text() formula. And the & is used to join them.

You can use Filter like this:
= filter ( A2:I,B2:B="Buying",
text(A2:A,"yyyy.mm.dd.hh")>=text(now(),"yyyy.mm.dd.") & "08",
text(A2:A,"yyyy.mm.dd.hh")<=text(now()+0,"yyyy.mm.dd.") & "16"
)
begin from row 2 because in row 1 are column titles
Remarks1 : 08 & 16 are for limit hour begin from 08 and last 16, can be changed
Remarks2 : +0 only this day, if change to +1 this day and tomorrow and so on

Related

How to do less than equal to for date in sqlite

When I try to run the below query I get no result even though the date time is same as that in the table.
The BillDateTime column is of TEXT Data type
SELECT * FROM BillingTransaction WHERE BillDateTime <= datetime('2021-09-19 07:21:31.371766')
BillNo
VechicleNo
BillDateTime
LoadType
BillAmount
FirstWeight
SecondWeight
1
ka04sdfl
2021-09-19 07:21:31.371766
EMPTY
30
400
0
The function datetime() strips off everything after the seconds, because it is equivalent to:
strftime('%Y-%m-%d %H:%M:%S', ...)
The result of:
SELECT datetime('2021-09-19 07:21:31.371766')
is:
2021-09-19 07:21:31
So your code is equivalent to:
SELECT * FROM BillingTransaction WHERE BillDateTime <= '2021-09-19 07:21:31'
and this is why you don't get any rows.
You don't need the function datetime(), because the values of the column BillDateTime are strings in the proper ISO format.
You can do direct comparisons with them:
SELECT *
FROM BillingTransaction
WHERE BillDateTime <= '2021-09-19 07:21:31.371766'

How to get final length of a line in a query?

I am just learning SQL and I got a task, that I need to find the final length of a discontinuous line when I have imput such as:
start | finish
0 | 3
2 | 7
15 | 17
And the correct answer here would be 9, because it spans from 0-3 and then I am suppsed to ignore the parts that are present multiple times so from 3-7(ignoring the two because it is between 0 and 3 already) and 15-17. I am supposed to get this answer solely through an sql query(no functions) and I am unsure of how. I have tried to experiment with some code using with, but I can't for the life of me figure out how to ignore all the multiples properly.
My half-attempt:
WITH temp AS(
SELECT s as l, f as r FROM lines LIMIT 1),
cte as(
select s, f from lines where s < (select l from temp) or f > (select r from temp)
)
select * from cte
This really only gives me all the rows tha are not completly usless and extend the length, but I dont know what to do from here.
Use a recursive CTE that breaks all the (start, finish) intervals to as many 1 unit length intervals as is the total length of the interval and then count all the distinct intervals:
WITH cte AS (
SELECT start x1, start + 1 x2, finish FROM temp
WHERE start < finish -- you can omit this if start < finish is always true
UNION
SELECT x2, x2 + 1, finish FROM cte
WHERE x2 + 1 <= finish
)
SELECT COUNT(DISTINCT x1) length
FROM cte
See the demo.
Result:
length
9

I have Year & Month but need to incorpate month number also

This is my value in the table : FY20 JAN
And i am looking for 'FY20 (M01) JAN'. How can convert like this in Oracle 11g SQL query ?
First you convert your string to a value of DATE type. Anything enclosed in double quotes is somewhat hard coded and TO_DATE function ignores them as long as they match the characters in the input in their specific locations. Here FY are in location (index) 1 and 2.
alter session set nls_date_format = 'yyyy-mm-dd';
select to_date('FY20 JAN', '"FY"yy MON') d from dual;
D
----------
2020-01-01
Then, you apply another function TO_CHAR to the date value we got above to get the desired output.
select to_char(
to_date('FY20 JAN', '"FY"YY MON')
, '"FY"yy "(M"mm")" MON'
) c from dual;
C
-----------------------
FY20 (M01) JAN

SQLITE Join on Date = Date + x

I am using the following insert query to create a comparison between two tables using the dates to join on.
INSERT INTO Comp_Table (Date, CKROne, CKRTwo, ChangeOne, ChangeTwo, State)
SELECT BaseTbl.Date, BaseTbl.CKR, CompTbl.CKR, BaseTbl.Change, CompTbl.Change,
CASE
WHEN BaseTbl.Change > 0 AND CompTbl.Change > 0 THEN 'positive'
WHEN BaseTbl.Change < 0 AND CompTbl.Change < 0 THEN 'positive'
ELSE 'inversely'
END AS 'Correlation'
FROM BaseTbl
JOIN CompTbl ON BaseTbl.Date = CompTbl.Date;
This works well. However, I would like to be able to join the tables with a lag. As in, the user can define if they want to do exact match on dates or if they want to use a date of one's occurrence plus a number and return the value from the latter date for comparison to the number to the former date. Pseudo code example:
User sets variable = 0 then
Join ComTbl On BaseTbl.Date = CompTbl.Date + 0;
User sets variable = 7 then
Join CompTbl On BaseTbl.Date = CompTbl.Date + 7;
(joins 2012-01-01 from BaseTbl to 2012-01-08 from CompTbl)
I tried to add days like you would in a Where clause ('+7 day'), but this didn't work. I also tried to using a Where clause with BaseTbl.Date = CompTbl.Date '+ 7 day' but that returned a 0 value also. How can this be accomplished in SQLite?
I think you can use the DATE() function to build the WHERE clause you want:
INSERT INTO ...
SELECT ...
FROM BaseTbl
INNER JOIN ComTbl
ON BaseTbl.Date = DATE(CompTbl.Date, '7 days')

Getting All the record of particular month - Building SQL Query

I need some help to build SQL Query. I have table having data like:
ID Date Name
1 1/1/2009 a
2 1/2/2009 b
3 1/3/2009 c
I need to get result something like...
1 1/1/2009 a
2 1/2/2009 b
3 1/3/2009 c
4 1/4/2009 Null
5 1/5/2009 Null
6 1/6/2009 Null
7 1/7/2009 Null
8 1/8/2009 Null
............................
............................
............................
30 1/30/2009 Null
31 1/31/2009 Null
I want query something like..
Select * from tbl **where month(Date)=1 AND year(Date)=2010**
Above is not completed query.
I need to get all the record of particular month, even if some date missing..
I guess there must be equi Join in the query, I am trying to build this query using Equi join
Thanks
BIG EDIT
Now understand the OPs question.
Use a common table expression and a left join to get this effect.
DECLARE #FirstDay DATETIME;
-- Set start time
SELECT #FirstDay = '2009-01-01';
WITH Days AS
(
SELECT #FirstDay as CalendarDay
UNION ALL
SELECT DATEADD(d, 1, CalendarDay) as CalendarDay
FROM Days
WHERE DATEADD(d, 1, CalendarDay) < DATEADD(m, 1, #FirstDay)
)
SELECT DATEPART(d,d.CalendarDay), **t.date should be (d.CalendarDay)**, t.Name FROM Days d
LEFT JOIN tbl t
ON
d.CalendarDay = t.Date
ORDER BY
d.CalendarDay;
Left this original answer at bottom
You need DATEPART, sir.
SELECT * FROM tbl WHERE DATEPART(m,Date) = 1
If you want to choose month and year, then you can use DATEPART twice or go for a range.
SELECT * FROM tbl WHERE DATEPART(m,Date) = 1 AND DATEPART(yyyy,Date) = 2009
Range :-
SELECT * FROM tbl WHERE Date >= '2009-01-01' AND Date < '2009-02-01'
See this link for more info on DATEPART.
http://msdn.microsoft.com/en-us/library/ms174420.aspx
You can use less or equal to.
Like so:
select * from tbl where date > '2009-01-01' and date < '2009-02-01'
However, it is unclear if you want month 1 from all years?
You can check more examples and functions on "Date and Time Functions" from MSDN
Create a temporary table containing all days of that certain month,
Do left outer join between that table and your data table on tempTable.month = #month.
now you have a big table with all days of the desired month and all the records matching the proper dates + empty records for those dates who have no data.
i hope that's what you want.

Resources