Ms Access query Where - sql

I am trying to find have to filter my query, in order to show me result from this month and the last month.
WHERE t.AllDate Between Date() - 60 and Date()
My code so far is this, but it roll back 60 days.
I need to have result for all the last month.
Thank you.

It might be simpler this way:
WHERE DateDiff("m", t.AllDate, Date()) < 2

If the question is
How to get the date of the first day of the previous month ?
You can do this in MS Access SQL :
DateSerial(Year(DateAdd('m',-1,Date())), Month(DateAdd('m',-1,Date())),1)
Right now it returns 1/04/2018
Your query becomes :
WHERE t.AllDate Between DateSerial(Year(DateAdd('m',-1,Date())), Month(DateAdd('m',-1,Date())),1)
and Date()
With this trick you will always get the right year and month, even if you run it in January.

Related

SQL if last day of pervious month has value change the beginning date of next month to start from that day

Need help with this sql code, I would like to roll back start day of new month if last day of pervious month has values.
Thanks
I would suggest nesting EOMONTH() in an IF statement.
Not sure what table(s) you're working with, but general layout would be:
IF(MAX(date_column) = EOMONTH(MAX(date_column), MAX(date_column), new_date)
Reference for EOMONTH():
https://www.sqlservertutorial.net/sql-server-date-functions/sql-server-eomonth-function/

How to decipher complex DATEADD function from MS Access

I have inherited a query from an old MS Access DB and cannot for the life of me figure out what was trying to be done in this date parameter function. I normally only use SQL and this seems a bit different. Can any one assist in describing what this logic is doing?
use pdx_sap_user
go
select po_number,
po_issue_date
from vw_po_header
where po_issue_date > getDate() And PO_issue_date < DateAdd("d",-1,DateAdd("m",8,DateAdd("d",-(Day(getDate())-1),getDate())))
You can de-obfuscate it a lot by using DateSerial:
where
po_issue_date > getDate() And
po_issue_date < DateSerial(Year(getDate()), Month(getDate()) + 8, 0)
First: there is no getDate() function in Access. Probably it should be Date() which returns the current date.
Now starting from the inner expression:
Day(Date()) returns the current day as an integer 1-31.
So in DateAdd("d", -(Day(Date())-1), Date()) from the current date are subtracted as many days as needed to return the 1st of the current month.
Then:
DateAdd("m", 8, DateAdd("d", -(Day(Date())-1), Date()))
adds 8 months to the the 1st of the current month returning the 1st of the month of the date after 8 months.
Finally:
DateAdd("d", -1,...)
subtracts 1 day from the date returned by the previous expression, returning the last day of the previous month of that date.
So if you run today 13-Sep-2019 this code, the result will be:
30-Apr-2020
because this is the last day of the previous month after 8 months.
I think the following:
Take the current date
Substract the current day of month -1 to get the first day of current month
Add 8 month to this
Substract 1 day to get the last day of the previous month
So it calculates some deadline in approx 8 months.
But I wonder how a PO issue date can be in the future...

Oracle SQL: Dynamic timeframe calculation

Greetings all knowing Stack.
I am in a bit of a pickle, and I am hoping for some friendly assistance form the hive mind.
I need to write a query that returns the difference in days between a registration date (stored in a table column) and the first day of the last September.
For example; assuming the query was being run today (24-10-2016) for a record with a registration date of 14-07-2010, I would want the script to return the difference in days between 14-07-2010 and 01-09-2016
However had I run the same query before the end of last August, for example on 12-08-2016, I would want the script to return the difference in days between 14-07-2010 and 01-09-2015.
I'm fine with the process of calculating differences between dates, it's just the process of getting the query to return the 'first day of the last September' into the calculation that is tripping me up!
Any input provided would be much appreciated.
Thankyou =)
Try this approach:
add four months to the current date
truncate this date to the first of year
subtract four months again
Add_Months(Trunc(Add_Months(SYSDATE, 4), 'year'), -4)
Hope this might help.
WITH T AS (SELECT TO_DATE('14-07-2010','DD-MM-YYYY') REG_DATE,
SYSDATE EXEC_DATE
FROM DUAL)
SELECT CASE WHEN TO_CHAR(EXEC_DATE,'MM') >= 9
THEN ADD_MONTHS(TRUNC(EXEC_DATE,'YEAR'),8)
ELSE ADD_MONTHS(TRUNC(ADD_MONTHS(EXEC_DATE,-12),'YEAR'),8)
END
- REG_DATE AS DIFF
FROM T;

How do I get the date of the last day of the previous month in the previous year in SQL?

I am writing a query with a where clause which needs to return the final day of the previous month in the previous year. So if I ran it today (18/06/2014), it would return 31/05/2013. If I ran it next month on 19/07/2014 it would return 30/06/2013 and so on.
I can use
select DATEADD(day,-1,DATEadd(MONTH,datediff(month,0,GETDATE()),0))
which returns the final date of last month but I can't work out how to adapt this to give me the date a year previously.
I can add - 365 to the end of the above code which does what I want but wouldn't account for leap years. Although I don't expect my reports to be in use for long enough for this to necessarily matter it would be good to find a solution that works nicely (and to think that they might be).
Any solutions greatly appreciated.
Just add another dateadd(YY, -1, {your code}) around your getdate() to roll the year back first, then look to previous month (per comments below).
select DATEADD(day,-1,DATEadd(MONTH,datediff(month,0,DATEADD(YY,-1,GETDATE())),0))

Make calc on dates using informix db

I want to put the following condition in my query ,but i don't know the correct syntax in informix to do that .
At least one year passed on his work date ..
So
I try some thing like that
b.work_date - CURRENT >= 12 -- 12 month
How to do that ?
You can do:
b.work_date <= CURRENT - 12 UNITS MONTH
You need to be careful with the CURRENT - 12 UNITS MONTH approach. It does not take the leap-day in February into account, and would explode with an Invalid day in date error if you ran it on 2012-04-29.
It is safer to write
b.work_date < TODAY - 365