SQL Last Three Months - sql

I have wrote SQL to select user data for last three months, but I think at the moment it updates daily.
I want to change it so that as it is now October it will not count Octobers data but instead July's to September data and change to August to October when we move in to November
This is the SQL I got at the moment:
declare #Today datetime
declare #Category varchar(40)
set #Today = dbo.udf_DateOnly(GETDATE())
set #Category = 'Doctors active last three months updated'
declare #last3monthsnew datetime
set #last3monthsnew=dateadd(m,-3,dbo.udf_DateOnly(GETDATE()))
delete from LiveStatus_tbl where Category = #Category
select #Category, count(distinct U.userid)
from UserValidUKDoctor_vw U
WHERE LastLoggedIn >= #last3monthsnew
How would I edit this to do that?

WHERE LastLoggedIn >= DATEADD(month, DATEDIFF(month, 0, GETDATE())-3, 0)
AND LastLoggedIn < DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)
The above statement will return any results in July till before the start of current month.

Referencing this answer to get the first day of the month:
How can I select the first day of a month in SQL?
You can detect the month limitations like so:
select DATEADD(month, DATEDIFF(month, 0, getdate()) - 3, 0) AS StartOfMonth
select DATEADD(month, DATEDIFF(month, 0, getdate()), 0) AS EndMonth
Then you can add that into variables or directly into your WHERE clause:
declare #StartDate datetime
declare #EndDate datetime
set #StartDate = DATEADD(month, DATEDIFF(month, 0, getdate()) - 3, 0)
set #EndDate = DATEADD(month, DATEDIFF(month, 0, getdate()), 0)
select #Category, count(distinct U.userid)
from UserValidUKDoctor_vw U
where LastLoggedIn >= #StartDate AND LastLoggedIn < #EndDate
Or:
select #Category, count(distinct U.userid)
from UserValidUKDoctor_vw U
where LastLoggedIn >= DATEADD(month, DATEDIFF(month, 0, getdate()) - 3, 0)
and LastLoggedIn < DATEADD(month, DATEDIFF(month, 0, getdate()), 0)

How about using BETWEEN ?
WHERE LastLoggedIn
BETWEEN
DATEADD(month, DATEDIFF(month, 0, GETDATE())-3, 0)
AND
DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)

Related

Pass current month date in parameter and get get prior month data

I tried this but no desired result in where clause
Convert(varchar(7), dateadd(mm, -1, getdate()), 120)
=Convert(varchar(7), #Paramtername, 120)
In SQL Server:
To get the start of the prior month, you can use
dateadd(month, datediff(month, 0, getdate() )-1, 0)
To get data for the prior month of a parameter you could use something like this:
where mydate >= dateadd(month, datediff(month, 0, #date_parameter )-1, 0)
and mydate < dateadd(month, datediff(month, 0, #date_parameter ) , 0)

Week of the Month in SQL

DECLARE #date DATETIME= GETDATE()
SELECT DATEDIFF(WEEK,
DATEADD(WEEK,
DATEDIFF(WEEK, 0,
DATEADD(MONTH, DATEDIFF(MONTH, 0, #date), 0)),
0), #date - 1) + 1
What is purpose of 0 as a parameter in the datediff() function?
The specific answer to your question is that the 0 is just a way to get the beginning of the month:
dateadd(month, datediff(month, 0, #date), 0)
This is one method to do this in SQL Server, because it does not offer a "date truncate" function. I prefer:
dateadd(day, 1 - day(#date), #date)
(Although admittedly this is a wee bit more complicated if #date has a time component.)
However, a much simpler way to do this is:
select (day(#date) - 1) / 7) as week_of_month
Below gets the number of months from a reference point
datediff(month, 0, #date)
Then it is used to add to the reference date back to reach the first day of the month
dateadd(month,
datediff(month, 0, #date),
0)
So it is used to find the first day of the current month
declare #date datetime = getdate()
select
getdate(),
datediff(month, 0, #date),
dateadd(month,
datediff(month, 0, #date),
0)

query for first and last day of month

If I have the name of the month, how can I have the first and last day of that month in SQL?
I have this query to returns the month names:
DECLARE #StartDate DATETIME,
#EndDate DATETIME;
SELECT #StartDate = '20110501'
,#EndDate = '20110801';
SELECT DATENAME(MONTH, DATEADD(MONTH, x.number, #StartDate)) AS MonthName
FROM master.dbo.spt_values x
WHERE x.type = 'P'
AND x.number <= DATEDIFF(MONTH, #StartDate, #EndDate)
Result:
Now, how can i get the first and last day of that months? changing the query.
If you want a more general and simple solution:
DECLARE #startdate AS DATETIME = GETDATE()
SELECT
DATEADD(MONTH, DATEDIFF(MONTH, 0, #startdate) , 0) as startMonth,
DATEADD(SECOND, -1, DATEADD(MONTH, 1, DATEADD(MONTH, DATEDIFF(MONTH, 0, #startdate) , 0) ) ) as endMonth
Gives the result:
startMonth endMonth
----------------------- -----------------------
2013-06-01 00:00:00.000 2013-06-30 23:59:59.000
Try this :-
DECLARE #StartDate DATETIME,
#EndDate DATETIME;
SELECT #StartDate = '20110501'
,#EndDate = '20110801';
SELECT DATENAME(MONTH, DATEADD(MONTH, x.number, #StartDate)) AS MonthName,
CONVERT(VARCHAR(25),
DATEADD(dd,-(DAY(DATEADD(MONTH, x.number, #StartDate))-1),DATEADD(MONTH, x.number, #StartDate)),101) as FirstDay,
CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,DATEADD(MONTH, x.number, #StartDate)))),DATEADD(mm,1,DATEADD(MONTH, x.number, #StartDate))),101) as LastDay
FROM master..spt_values x
WHERE x.type = 'P'
AND x.number <= DATEDIFF(MONTH, #StartDate, #EndDate)
Result :-
MonthName FirstDay LastDay
May 05/01/2011 05/31/2011
June 06/01/2011 06/30/2011
July 07/01/2011 07/31/2011
August 08/01/2011 08/31/2011
Result obtained taking the help from this query
This code gives you the first date of Current Month:
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
If you know the Month's number then:
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + #MonthNumber, 0)
Same goes for Month's end date :
SELECT DATEADD(MILLISECOND, -1,
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - ##MonthNumber+ 1, 0))

SQL - Get data based on months form a dateTime column

With SQL Server, I have a column with a launch date (dateTime). I want to report on everything that is being launched between all of last month (from viewing date) thru all of this month and next month.
So basically a full 3 month period.
What is the best way to write that?
Are you looking for something like this?
DECLARE
#StartDate DATETIME,
#EndDate DATETIME
SELECT
#StartDate = DATEADD(MM, DATEDIFF(MM, 0, GETDATE()) - 1, 0),
#EndDate = DATEADD(MM, DATEDIFF(MM, 0, GETDATE()) + 2, 0)
-- DATEADD(MM, DATEDIFF(MM, 0, GETDATE()), 0), -- beginning of this month
-- DATEADD(MM, DATEDIFF(MM, 0, GETDATE()) - 1, 0), -- beginning of last month
-- DATEADD(MM, DATEDIFF(MM, 0, GETDATE()) + 1, 0) -- beginning of next month
-- DATEADD(MM, DATEDIFF(MM, 0, GETDATE()) + 2, 0) -- beginning of two months from now
SELECT
*
FROM
[Table]
WHERE
[LaunchDate] >= #StartDate
AND [LaunchDate] < #EndDate
This will give you all the results starting from the beginning of the previous month and before the beginning of two months from now (a full 3 month range)
Maybe something like SELECT /*what_you_want*/ from launches WHERE lauchDate BETWEEN DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()) - 1, '19000101') AND DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()) + 2, '19000101')
SELECT Foo
FROM Bar
WHERE LaunchDate >= DATEADD(mm, -1, GETDATE())
AND LaunchDate <= DATEADD(mm, 1, GETDATE())

SQL Server 2005 select fields from a certain time frame

Right now I'm using this command to retrieve all fields from the current day:
SELECT COUNT(*)
FROM [SecureOrders]
WHERE DateTime >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
AND
DateTime < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 1)
However, I want to be able to get the fields that were entered between noon yesterday and noon today - how can I go about doing that?
0.5 is noon (eg half a day)
WHERE DateTime >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), -0.5)
AND
DateTime < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0.5)
DECLARE #NoonToday DATETIME;
SET #NoonToday = DATEADD(HOUR, 12, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP));
SELECT COUNT(*) FROM [SecureOrders]
WHERE [DateTime] >= DATEADD(DAY, -1, #NoonToday)
AND [DateTime] < #NoonToday;
it might seem ugly but should work
SELECT
COUNT(*)
FROM
[SecureOrders]
WHERE
DateTime >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) - 0.5 AND DateTime < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 1) + 0.5
A variation on #Aaron Bertrand's solution, without declaring a variable and without treating a non-zero integer value as a date:
SELECT COUNT(*)
FROM SecureOrders o
CROSS JOIN (
SELECT DATEADD(HOUR, DATEDIFF(DAY, 0, GETDATE()) * 24 + 12, 0)
) AS d (TodayNoon)
WHERE o.DateTime < d.TodayNoon
AND o.DateTime >= DATEADD(DAY, -1, d.TodayNoon)