Getting Last Day of Month (2 Months Ago) in ODBC - sql

I'm trying to get the last day of 2 months ago on an ODBC server but can't seem to get it quite right as the EOMONTH built-in function of SSMS is not functional in ODBC.
The part of the code that I currently have in my WHERE clause returns the 1st day of the last month. I'm trying to get the day before that, which would be as described, the last day of 2 months ago.
Here's the code that I'm currently using:
WHERE x_date = cast(dateadd(month, -1, dateadd(day, 1 - day(getdate()), getdate())) as date)
Currently, we are May 7th, 2021. That WHERE clause returns me April 1st, 2021. I would like it to return me March 31st, 2021.
I've tried other WHERE clauses, without any positive results.
Any help would be greatly appreciated.
Thank you.

you can use EOMONTH function in sql server :
SELECT EOMONTH(GETDATE(), -2)
if it is not supported then this should work:
SELECT DATEADD(dd,-(DAY(DATEADD(mm,-1,getdate()))),DATEADD(mm,-1,getdate()))
by open day if you mean weekdays:
select case DATENAME(weekday , <above date>)
when 'Saturday' then dateadd(day , 2 , <above date>)
when 'sunday' then dateadd(day , 1 , <above date>)
else <above date>

Related

SQL SELECT SUM BETWEEN 1 Year from now to start ot the year

so I'm kinda stuck with a little query to get the SUM of the time period I want. So it already works but I want that it's automatically between 1 year from now and the beginning of the year.
SELECT SUM(N_EXCL), S_CUSTNO, S_CUSTNAME1, D_INVOICEDATE
from sao.INVOICE_P i
WHERE D_INVOICEDATE > DATEADD (year, -1, getdate ())
AND D_INVOICEDATE < DATEADD (month , -6 ,Getdate () )
GROUP BY S_CUSTNO, S_CUSTNAME1, D_INVOICEDATE
Currently I would have to change the query every month but I try to find a way to avoid it.
Ok. Maybe i wasn't clear enough. My problem is the -6 i have to write down. I want to switch it to a parameter that will automatically change up the date so that i get data from 01.01.2021 - 22.06.2021. So same date, but different year.
If I understand your question, you should be able to do this:
SELECT SUM(N_EXCL), S_CUSTNO, S_CUSTNAME1, D_INVOICEDATE
from sao.INVOICE_P i
WHERE D_INVOICEDATE between DATEADD(year, -1, getdate()) AND DATEFROMPARTS(getdate(), 1, 1)
GROUP BY S_CUSTNO, S_CUSTNAME1, D_INVOICEDATE
So instead of using where and I'm using where between. And I'm checking for todays date last year till 1st of January current year.
If you need from 1st of January previous year you can use:
between DATEFROMPARTS(year(getdate() -1), 1, 1) AND DATEADD(year, -1, getdate())

not getting previous month record using datepart in sybase

Using below depart syntax to fetch for previous month record, it is working fine till previous year,however it is giving 0 value in January month.How can we get pervious month with date part even if year is change ?
DATEPART(month(GETDATE()) -1
I understand that I used another type of DB, but I want to give a hint. I am using sql server 2019.
Firstly, you need to substitute date and only then take datepart from it.
Queries:
--dateadd -1 would subtract 1 from current month
--(Jan - 1 2022), would be December 2021
select datepart(month, dateadd(month, -1, getdate()))
--also date add covers internally the problem with 30,31 days.
--May always has 31 days, April 30. So -1 subtraction from 31th of May,would result in 30th of April.
select dateadd(month, -1, cast('2021-05-31 10:00:00' as datetime))

Data Preparation End OF Every Month - Moving Over 12 Months

I have data prep procedure in SQL. I want to have data preparation at the end of every month
Say I want the procedure run on last day of month e.g. on 31 January 2020 it should prep data from 1 January to 31 January.
So it's kind of moving window over all months of the year. Because I need data for evaluation at the end of each month.
I tried this, however, this does not give automation. Its sort of manual running end of every month
select '2020-10-01' as beginDate_AnalysisWindow
, '2020 -01-31' as endDate_AnalysisWindow
into #AnalysisWindow --create temporary table #AnalysisWindow
I also tried the following, however, I’m not sure if it does for the whole month or just one day?
SELECT START_OF_MONTH_DATE AS beginDate_AnalysisWindow
,END_OF_MONTH_DATE AS endDate_AnalysisWindow
INTO #AnalysisWindow
FROM [dbo].[Date] WITH (NOLOCK)
WHERE DATE = DATEADD(dd, - 1, CAST(GETDATE() AS DATE))
Could someone pls help me/give me some suggestions.
Thanks in advance
If you want the last day of the current month, use eomonth():
WHERE DATE = CONVERT(date, EOMONTH(GETDATE()))
Note: This assumes that the date column has no time component. If that is the case:
WHERE CONVERT(date, DATE) = CONVERT(date, EOMONTH(GETDATE()))
SQL Server will still use an index (if available) even for this type of conversion.
EDIT:
To get the current months data, one method is:
WHERE DATE <= CONVERT(date, EOMONTH(GETDATE())) AND
DATE > CONVERT(date, EOMONTH(GETDATE(), -1))
The second argument to EOMONTH() is a months offset.
You can also try:
where getdate() <= EOMONTH(GETDATE()) AND
getdate() > DATEADD(DAY, 1, EOMONTH(GETDATE(), -1))
Instead of getdate(), you can use your date column.

Conditional SQL Query to fetch last date of the quarter on the basis of few condition

Basically a report is needed once a quarter, and the analysis date(any date) is the last date of the preceding quarter. So if we are loading on 10 July, “Analysis Date” would be 30 June (of the same year). HOWEVER - it is possible that the users will want to load the data early, in order to see what may need to be addressed by the end of the quarter. In that case they may want to load it on 20 June, and “Analysis Date” will still be 30 June.
How do i write the query for these two conditions, considering there will be only one month in advance for which this specific constraint is needed.
This should put you on the right track. If you have a version of SQL Server older than 2012 you will need to use something instead of EOMONTH
CREATE FUNCTION dbo.GetAnalysisDate(#date DATE)
RETURNS DATE
AS
BEGIN
IF MONTH(#date) % 3 = 0 RETURN EOMONTH(#date)
-- else
RETURN DATEADD(dd, -1, DATEADD(qq, DATEDIFF(qq, 0, #date), 0))
END
GO
-- tests
-- these should return 20181231
SELECT dbo.GetAnalysisDate('20190101')
SELECT dbo.GetAnalysisDate('20190120')
SELECT dbo.GetAnalysisDate('20190220')
-- these should return 20190331
SELECT dbo.GetAnalysisDate('20190320')
SELECT dbo.GetAnalysisDate('20190401')
SELECT dbo.GetAnalysisDate('20190420')

SQL Server 2008 SQL Query to retrieve 'working week' (Tuesday to Tuesday)

There are quite a few posts on this topic, but we are still struggling to get some SQL that works for the period and the posts are just confusing.
What we want to do is return all records where timestamp is >= start of working week which is a Tuesday. Could someone advise where we are going wrong.
i.e Yesterday it was 27th October, so when we ran our query it would return all results from 21st October (the previous Tuesday). Today it is 28th, so when we now run our query, it will only return results from the 28th. Tomorrow when we run it, we will get 28th and 29th....etc.
timestamp >= dateadd(day, -4-datepart(dw, getdate()), CONVERT(date,getdate()))
Note: this query worked fine when we ran it yesterday, so I am wondering if it changes by the day?
Mod 7 please:
select
dateadd(day, -(datepart(dw, getdate() ) + 4)%7, getdate())
,dateadd(day, -(datepart(dw, '2014-10-28' ) + 4)%7, '2014-10-28') --2014-10-28
,dateadd(day, -(datepart(dw, '2014-10-27' ) + 4)%7, '2014-10-27') --2014-10-21
,dateadd(day, -(datepart(dw, '2014-10-29' ) + 4)%7, '2014-10-29') --2014-10-28
UPDATE
According to #hvd's comment, when we use these approach, we assuming the start day of week is Sunday by default of U.S.English. Further information in this document.
SET DATEFIRST 7 can ensure this approach to be correct.
Finally understood your points, you should provide an extra parameter here to point out which day is the beginning of working week, then it will be easy to calculate the days