SQL connection within Excel- date range - sql

I need to be able to look at the previous 3 months of data using a SQL statement within Excel.
I have the below code which I've been able to build but it doesn't do what I need 100%.
It gives me the last 3 months and the last date of the 4th month and the first date of the current month.
I could use Excel VBA and delete out data that is the first of current and last of the month 4 months ago.
However, I was wondering if anyone could help correct the below.
I've tried using EOMNTH and this brings an error up and I cant seem to use a DECLARE statement either.
If the below could be changed to only bring back information from the last 3 months (Jan, Feb and March) that would be great.
If someone could explain how I could change the below for only dates within the previous month that would be helpful too.
where T1."Receive_Date" between DATEADD(dd, -DAY(DATEADD(mm, -4, getdate())), DATEADD(mm, -3, getdate()))
and DATEADD(dd, -DAY(getdate()) + 1, getdate())

Related

SQL Query Last Three Full Months (Excluding Current Month)

I have a database with millions of rows and many years of data. I'm currently using PowerBI to query the data (ODBC connection), but I don't want to pull everything. (Recent convert from Crystal.) Instead, I'm trying to write a clause into my WHERE statement to only query the last three full months of data (excluding the current month.) So, today being Jan 25,2023 I want to capture Oct 1 2022 - Dec 31 2022. I would then schedule this so it always pulls the last three months.
My dataset does contain a field that stores the date so I'm trying to write something like the below that I'm able to use to pull previous days.
"Table"."OrderDate" >= DATEADD(d,DATEDIFF(d,0,CURRENT_TIMESTAMP)-1,0) AND
"Table"."OrderDate" < DATEADD(d,DATEDIFF(d,0,CURRENT_TIMESTAMP),0))
Everything so far that I've tried (and found online) gives odd results. Pulls Nov - Jan and the like.
Thank you for any help you can provide.
You can use the following query to pull the last three full months of data. This query uses the DATEADD and DATEDIFF functions to calculate the date range for the last three full months.
WHERE "Table"."OrderDate" >= DATEADD(month, DATEDIFF(month, 0, CURRENT_TIMESTAMP) - 3, 0) AND "Table"."OrderDate" < DATEADD(month, DATEDIFF(month, 0, CURRENT_TIMESTAMP), 0)

DATEADD to return data in the last 3 months but I want full month of data

So my query aims to grab all data in the last 3 months, but only returns data when it is a full month.
I have tried:
WHERE Created_Date > DATEADD(MONTH, -3, GETDATE())
or
WHERE Created_Date > DATEADD(DAY, -90, GETDATE())
Both ways return the data in the last 3 months starting from current date. But the thing is, since my query wants to get aggregated data, so if today is 8th Aug, 3 months dating back means May has not got the full month of data (from 1st to 31st), so the aggregated data is not fully reported in the results. Does this make sense?
Is there any other way to return the full month data?
I know that we can use #startOfCurrentMonth like in here but this is 3 months we are aiming to get.
To get the days of the current month plus three full months back, simply subtract four months and get that month's last day. Then take any dates after that day.
WHERE created_date > EOMONTH(DATEADD(MONTH, -4, GETDATE()))
If created_date is a misnomer and contains datetimes instead of dates, add a day and include that:
WHERE created_date >= DATEADD(DAY, 1, EOMONTH(GETDATE(), -4)))

Report that updates yearly

I have created a report that is supposed to look at the number of baptisms at our church for the ministry year. The Ministry year runs from Aug 1 - July 31. I currently have the report set to tell me the names of anyone that has a baptism date greater than 8/1/2016. But I would need to change that year each year for it to report properly. so I wanted to use a Case statement to have it update each year, but i am getting an error message with this: (The error is in the where clause, so I didn't include the entire report)
WHERE (P.organization_id = 1) AND
((CandidateProcesses_BaptismDate68.datetime_value) between (
case
When datepart(month, getdate()) < 8 then ('8/1/'+ datepart(year, getdate()))
When datepart(month, getdate()) >7 then ('8/1/'+
datepart((year,getdate())-1))End) and Getdate())
Does anyone see why I am getting an error?
Thanks!
You are getting an error trying to add a string and a number. You could fix that using datename() rather than datepart(). But, I think this is a simpler approach:
WHERE (P.organization_id = 1) AND
year(dateadd(month, -7, CandidateProcesses_BaptismDate68.datetime_value)) = year(dateadd(month, -7, getdate()))
This subtract 7 months to get the "ministry year" and then compares that to the current date minus seven months. That is, it subtracts 7 months and then normalizes on the calendar year.
This is a bit more expensive than your version, because it cannot use an index on CandidateProcesses_BaptismDate68(datetime_value). However, I doubt the database of baptisms is so large that the query will take very long anyway. (If that is an issue, then your version can be made to work with some simple modifications.)

How to get YTD data in SQL

where (au.[DateTime]>= DATEADD(WK,-12,GETDATE()) AND au.[DateTime]< GETDATE())
--gets everyday for the last 12 weeks
How can I modify to get every day for YTD?
where au.[DateTime] between DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)
AND DATEADD(ms,-2,DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0))
This will get you the first day of the current year. A point i am not sure you need the And au.datetime ... since it is YTD it cannot have tomorrow data yet ? I tried -1 MS but for some reason it doesn't work. possible question.

Get Week Before Last

Hi I am trying to pull data which will excluded the previous 2 weeks and include the 2 weeks beofre that.
so for example if today is the 31st of the month i want to exclude all data between the 15th and 31st and want to see only the 1st to the 15th
AND E.EventCreatedDateD between DATEADD(day,-14,GETDATE()) AND DATEADD(day,-31,GETDATE())
You are getting 0 values from your statement because the dates are backwards. Put the earlier date first:
AND E.EventCreatedDateD between DATEADD(day, -31, GETDATE()) AND DATEADD(day, -14, GETDATE())
The order of the expressions for the between is important. The lower value always needs to be first and the higher value second.