Is there a dateadd scalar function in Google Query Language? - google-sheets-query

I am trying to select rows which are for today or alternatively for tomorrow:
Formula for today is easy (and tidy):
=QUERY(A1:B20,"select B where A = toDate(now())")
Tomorrow seems more ugly:
=QUERY(A1:B4,"select B where A = date '"&TEXT(TODAY()+1,"yyyy-mm-dd")&"'",0)
I searched the GQL documentation for a dateAdd function but couldn't find one. I tried various things like:
=QUERY(A1:B4,"select B where A = toDate(dateadd(now(),1,day))")
in case there was an undocumented dateadd function but no joy. Is there a pretty way of doing this or am I stuck with ugly?
example in:
https://docs.google.com/spreadsheets/d/1azvUwtvWikOgmZfVIs_gjNw3r8Tj4SywVjRnauNUFD8/edit?usp=sharing

I think you were close, but this formula seems to work:
=QUERY(A1:B4,"select B where A = date '"& TEXT(NOW(),"yyyy-mm-dd" & "'"))
That works for today. For tomorrow just add one to NOW(), so you get:
=QUERY(A1:B4,"select B where A = date '"& TEXT(NOW()+1,"yyyy-mm-dd" & "'"))
Let us know if this is helpful, even if perhaps "ugly".

Related

How do I write a query that works in both HSQLDB and MySQL to add days to a date in the WHERE clause?

I'd like to add a specific number of days to a date column and then compare that to the current date. Is there any way I can achieve this that is compatible with both HSQLDB (for testing) and MySQL (production)?
To make things even more complicated, I am using Hibernate.
Don't know the background of your problem, but can you do it the other way around: compare dae column with current date substracted by number of days?
I meant something like this in hql (I'm using jpql in this example, if you prefer JPA interface):
TypedQuery<SomeClass> q = getEntityManager().crateQuery(
"select s from SomeClass s where s.date = :date", SomeClass.class);
Calendar now = Calendar.getInstance();
// subtract number of days from current date, e.g. 10 days
now.add(Calendar.DAY, -10);
q.setParameter("date", now);
List<SomeClass> = q.getResultList();
This approach is database-agnostic, but of course works only for trivial cases, in some mor e complicated cases it will not work.

Pulling prior year data

I am working on a query to pull all turnover in the past calendar year that is going to be used daily. Rather than going in and having to change the date each time I would just like to be able to run the query and have it automatically only pull the last 365 days worth of data. The code itself looks like:
SELECT O867IA_VJOBHST.SYS_EMP_ID_NR, O867IA_VJOBHST.REC_EFF_STT_DT, O867IA_VJOBHST.EMP_ACN_TYP_CD
FROM O867IA_VJOBHST
WHERE (((O867IA_VJOBHST.EMP_ACN_TYP_CD)="HIR"));
Where the REC_EFF_STT_DT is the date the ACN_TYP_CD occurred, in this case when they were HIR (Hired)
Any Ideas?
Access SQL provides Date() and DateAdd() functions. You can work out what you need from those functions in the Immediate window ...
? Date()
9/9/2013
? DateAdd("d", -365, Date())
9/9/2012
Then you can filter REC_EFF_STT_DT on the same date range in a query like this ...
SELECT o.SYS_EMP_ID_NR, o.REC_EFF_STT_DT, o.EMP_ACN_TYP_CD
FROM O867IA_VJOBHST AS o
WHERE
o.REC_EFF_STT_DT BETWEEN DateAdd('d', -365, Date()) AND Date();

How to set the year part of a field using a query

Evening,
I have created a query that is suppose to change ONLY the year part of a Date/Time field to 1900 when a person is 89 years or older. The query that follows compiles fine but when run it complains about a Type Conversion failure and removes the entire value from the records affected.
The query:
UPDATE tblTestForDOB
SET tblTestForDOB.[PT_BirthDate] = DateValue( (day([PT_BirthDate])/month([PT_BirthDate])/1900) )
WHERE Year(tblTestForDOB.[PT_BirthDate]) <= Year(Date())-"89";
According to the MS Help (F1 over the function):
The required date argument is normally a string expression representing a date from January 1, 100 through December 31, 9999. However, date can also be any expression that can represent a date, a time, or both a date and time, in that range.
Is that not what I'm doing? I also tried placing the " " & before the values inside the DateValue function and that did the same thing
(to ensure that it was a string that was passed)
So how do I go about it? Should I use CDate to convert the value to a Date and then proceed that way? If so what is the correct syntax for this?
Thanks
P.S The field is of Short Date format. Also note that I don't want to take the long way around and use VBA for the whole thing as that would involve opening record sets and so on...
It appears you're trying to give DateValue a string, but that's not what's happening. There may be more going on that I don't understand, so I'll just show you an Immediate window session which may contain something you can build on.
PT_BirthDate = #1923-6-1#
? PT_BirthDate
6/1/1923
? DateDiff("yyyy", PT_BirthDate, Date())
90
' this throws error #13: Type mismatch ...
? DateValue( (day([PT_BirthDate])/month([PT_BirthDate])/1900) )
' it will work when you give DateValue a string ...
? DateValue("1900-" & Month(PT_BirthDate) & "-" & Day(PT_BirthDate))
6/1/1900
' or consider DateSerial instead ...
? DateSerial(1900, Month(PT_BirthDate), Day(PT_BirthDate))
6/1/1900

How to iterate over table in Access 2010

I would like to iterate over the rows in my Payment table. User chose for what month and year has wants to book and I want to check in the each raw if this house was booked for this year and month. I want to compare if
HouseID == chosenHouseID && BookingMonth == chosenBookingMonth &&
BookingYear==chosenBookingYear.
If this is true it should pop out message box with info that house was already booked for this month. Also if user chose more than one month i.e. numMonths would be 3, it should increment value of the month (which is a text) it should go to the next value (if there is no next value then it should do mod 12) and do the checking again. Maybe it will be necessary to switch data type of BookingMonth to numeric?
However I hope I was clear what I want to do. I have experience with Java, C, Python and Visual Basic, but I did not do much in Access so it is quite confusing. I could not find the any useful info how to perform this operation. Please advise me on my issue.
Thank you
Yes, you definitely should store the [BookingMonth] as numeric. Maintaining a "month" column as text will be a nuisance in the long run, since "August"<"January" and "12"<"2". You'd have to do at least a certain amount of juggling to convert the text values to numeric values, so make like easy for yourself and just maintain them as numeric. (Note that you can always format them as text if you want to use them in reports.)
As for your search requirements, if the user supplies a [chosenBookingYear], [chosenBookingMonth], and [numberOfMonthsToBook] then you can use the VBA DateAdd function to derive [endOfBookingYear] and [endOfBookingMonth] safely, accounting for "next year" values...
endOfBookingYear = Year(DateAdd("m", numberOfMonthsToBook - 1, DateSerial(chosenBookingYear, chosenBookingMonth, 1)))
...and...
endOfBookingMonth = Month(DateAdd("m", numberOfMonthsToBook - 1, DateSerial(chosenBookingYear, chosenBookingMonth, 1)))
Finally, to perform the lookup without looping through individual rows you can concatenate [BookingYear] and [BookingMonth] together to create something like "2013/05" using...
BookingYear & "/" & Format(BookingMonth, "00")
...so then you can create a SELECT query something like this:
SELECT * FROM Payment
WHERE HouseID = chosenHouseID AND
(
(BookingYear & "/" & Format(BookingMonth, "00"))
BETWEEN (chosenBookingYear & "/" & Format(chosenBookingMonth, "00"))
AND (endOfBookingYear & "/" & Format(endOfBookingMonth, "00"))
)

ASP AND MS ACCESS

set rs5 = objconn.execute("Select Sing from LeaveEntitlement where MonthEntitle = '"& Month(ttodate)& " ' ")
do until rs5.eof
if rs5("Sing") then
s = rs5("Sing")
Loop
In the Database table LeaveEntitlement, MonthEntitle is a Field Nmae(Text Data type) from January...December.Sing is another Field(Number Data type) with values for each Month.But
S is returning nothing.
Can help for solving this.
Oded, Thanks a lot for solving my issue.
Just taking a stab in the dark it looks like you have an extra space at the end of your sql where you put in the month name. Probably not matching any records.
There are probably no rows in the table corresponding to the month you are searching on.
Have you checked directly against the database that the query is returning any rows?
Edit:
It appears that you are using month names in your database, however the Month function returns a month number. You are enclosing that in ', so you are sending a string with the number in the query - this is why you do not get a type error and why no results are being returned.
You can use MonthName with Month:
where MonthEntitle = '" & MonthName(Month(ttodate)) & "' "