August 18, 2014 at 2:29 pm
Hello all,
So I am trying to figure out a way to present the number of records for a 3 month window and for the same date. I know not all months have 31 days so it would need to be smart enough so it does not error out. This is kinda the manual way to help you understand what I am looking for:
select count(learner_id) TheCount, month(creation_date) TheMonth from learner_info
where creation_date between '8/1/2014' and '8/18/2014'
or creation_date between '7/1/2014' and '7/18/2014'
or creation_date between '6/1/2014' and '6/18/2014'
group by month(creation_Date)
Keep in mind, tomorrow all three dates will need to be the 19th and the next day the 20th and so on.
Thank you in advance.
Kam
September 5, 2014 at 9:56 am
Give this a try: https://www.katieandemil.com/t-sql-first-day-of-previous-month?tab=article
put these in variables:
SELECT DATEADD(m,-1, Dateadd(d,1-DATEPART(d,getdate()),GETDATE()))
SELECT DATEADD(m,-2, Dateadd(d,1-DATEPART(d,getdate()),GETDATE()))
SELECT DATEADD(m,-3, Dateadd(d,1-DATEPART(d,getdate()),GETDATE()))
SELECT DATEADD(m,-1, GETDATE())
SELECT DATEADD(m,-2, GETDATE())
SELECT DATEADD(m,-3, GETDATE())
September 5, 2014 at 3:06 pm
khott (8/18/2014)
Hello all,So I am trying to figure out a way to present the number of records for a 3 month window and for the same date. I know not all months have 31 days so it would need to be smart enough so it does not error out. This is kinda the manual way to help you understand what I am looking for:
select count(learner_id) TheCount, month(creation_date) TheMonth from learner_info
where creation_date between '8/1/2014' and '8/18/2014'
or creation_date between '7/1/2014' and '7/18/2014'
or creation_date between '6/1/2014' and '6/18/2014'
group by month(creation_Date)
Keep in mind, tomorrow all three dates will need to be the 19th and the next day the 20th and so on.
Thank you in advance.
Kam
I don't know if your creation_date column has times in it but remember that BETWEEN is INCLUSIVE of both end points.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 8, 2014 at 11:34 am
Also, if you are looking to dates to start at 12:00:00 AM, for the times you will need to use the convert and cast date/time styles. Ex. select CONVERT(varchar(10),DATEADD(MM,-1,GETDATE()-DATEPART(dd,GETDATE())+1),101)
September 30, 2014 at 2:41 am
Assuming time part is not to be considered and that you need this for only 3 date spans (if you need for more one could Recursive CTE for this) :
-- The two dates for first condition
select DATEADD(m,DATEDIFF (m,'19000101',GETDATE()),'19000101')
select GETDATE()
-- The two dates for second condition
select DATEADD(m,-1,DATEADD(m,DATEDIFF (m,'19000101',GETDATE()),'19000101'))
select DATEADD(m,-1,GETDATE())
-- The two dates for third condition
select DATEADD(m,-2,DATEADD(m,DATEDIFF (m,'19000101',GETDATE()),'19000101'))
select DATEADD(m,-2,GETDATE())
The only variable in these 6 dates is getdate() (which of course can be replaced with any other date u want)...
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply