How to Target Last Month in SQL Query - sql

I'm using this code in an SQL query
WHERE [Date] >= DATEFROMPARTS(DATEPART(year,GETDATE()),DATEPART(month,GETDATE())-1,DATEPART(day,GETDATE()))
AND [Date] <= EOMONTH(DATEFROMPARTS(DATEPART(year,GETDATE()),DATEPART(month,GETDATE())-1,DATEPART(day,GETDATE())));
The problem is come 2020 the December query will through up an error
The code I posted manages the dates between which data will be returned. It looks at the date the code is run and choose that day from last month till the end of last month. What I need is dates from the 1st till the last day of the month prior to the one this code is called in.
I will be working on this issue tomorrow, it will be interesting to see what solutions other people can come up with.

try this
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 1, 0),
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) - 1
it will get you the previous month start and end date

If you want the previous month:
where date >= dateadd(month, -1, datefromparts(year(getdate(), month(getdate(), 1))) and
date < datefromparts(year(getdate(), month(getdate(), 1))
This simply checks that it is before the first of the this month and then subtracts a month from that.

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())

How to pull week to date data in SQL Server?

I'm trying to pull week to date data for a recurring report that needs to go out daily. For eg: report that goes out Monday needs to have data for Monday, report that goes out Tuesday will have data for Monday and Tuesday etc. for the current week.
I know how to pull last X days data with :
my_date > DATEADD(day, -6, GETDATE())
How do I pull only week to date?
Use below SQL code in where condition:
where
my_date > DATEADD(dd, -((DATEPART(WEEKDAY, mydate) + 5) % 7), DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0))
The correct adjustment requires a bit of extra modular arithmetic and so the proper number of days to adjust backward works out to the expression below. I am also assuming that datefirst is set to Sunday:
-((DATEPART(WEEKDAY, mydate) + 5) % 7)
If you just subtract two from the day number given by datepart() then you'll end up with a negative number on Sundays which then cause dateadd() to jump forward. You can see a listing of dates and comparison here:
https://rextester.com/IUY88999

SQL update date to first of next month plus three months

I have this query:
update courseRights
set courseRightsLevelExpires = DATEADD(MM,3,courseRightsLevelExpires)
This works fine but what I actually need is to extend to three months from the first day of next month. For example if the rights are expiring today, May 23rd, I need to update to June 1st + 3 months.
Is it possible to do that in one query?
Update
Because it was flagged as duplicate with another question, I'm updating the content to say that I'm not only looking for the first date of next month but I need to add three months to that date.
To get the first day of next month:
DATEADD(m, DATEDIFF(m, -1, current_timestamp), 0)
Three month's after that:
DATEADD(m,3,DATEADD(m, DATEDIFF(m, -1, current_timestamp), 0))
So, if courseRightsLevelExpires holds the base date you're working from:
DATEADD(m,3,DATEADD(m, DATEDIFF(m, -1, courseRightsLevelExpires), 0))
You can use eomoth() function :
update courseRights
set courseRightsLevelExpires =
dateadd(mm, 3, dateadd(dd, 1, eomonth(courseRightsLevelExpires)));
I will do:
set nextmonthdate = last_day (your-date) + 1 day

Week of quarter calculation SQL

I am trying to find the week of quarter from a cal_date_d table using sql. My table has cal_date, fiscal quarter start and end date. I also have a column which has the fiscal_week_of_year. and my fiscal year starts from feb.
However the closest query i've got to resolve this issue is below:
select datepart(week, DATEADD(MONTH,-10,cal_date)) - ((DatePart(quarter, DATEADD(MONTH,-10,cal_date))-1) *13),
fiscal_week_of_year,
weekofqtr,
cal_date
from cal_date_d_tst
Now the first week result i am always getting is 0. I am not sure where I am going wrong.
help me out on this one..
If you have fiscal_quarter_start, then doesn't this work?
select 1 + datediff(day, fiscal_quarter_start, cal_date) / 7
This just calculates the number of days into the quarter and divides by 7.
If you base your query on DATEPART(Quarters) use the following.
DATEDIFF(WEEK, DATEADD(MONTH, DATEDIFF(MONTH, 0, '2012-03-17'), 0), '2012-03-17') +1 AS WeekNoMonth,DATEDIFF(WEEK, DATEADD(QUARTER, DATEDIFF(QUARTER, 0, '2012-03-17'), 0), '2012-03-17') +1
Source

Trying to track progress this month vs Previous

This sounded simple but it doesnt work like I thought.
I am trying to track progress for Month to date
Here is my code for MTD
Startdate>=DATEADD(MONTH, DATEDIFF(MONTH, 0, Convert(date, getdate())), 0)
Its extremely simple.
For Last Months Start I use:
Startdate>= DATEADD(month, DATEDIFF(month, -1, getdate()) , 0)
as the start date
and for the current marker I used this:
Enddate>=DATEADD(month, DATEDIFF(month, 0, getdate()-30), 0)+datepart (day,getdate())
Ideally I want to know that if today is the 5th. How many sales took place in that window.
then I want to also know how we did in the same period the last month.
My problem is I find on months with 31 days that follow months with 29 days I have issues.
Is there a function that gives me the same date a month ago ?
dateadd(month, -1, getdate())
i.e. dateadd(month, -1, '20150330') returns '2015-02-28'
If exact day doesn't exist in previous month returns last day of month.