Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RE: How to calculate how many days in the last 3 months 1

Status
Not open for further replies.

allyne

MIS
Feb 9, 2001
410
US
Hello,

I'm sure this is simple but can seem to get it. I need a query to calculate how many days there are in the last 3 months. This will be the last 3 rolling months (The tricky part).

Any help is appreciated

Thanks!
 
This should put you down the right path....maybe not exact depending on your business case (adding one to one of the values might be needed to catch dates at the start/ending values). Please let me know if/how this does not find your case or you need an explanation.

Code:
DECLARE @InputDate DATETIME
SET @InputDate = GETDATE()

SELECT
	DATEADD(MONTH, -3, DATEADD(DAY, 1 - DAY(@InputDate), @InputDate)) 'StartDate'
	, DATEADD(DAY, 1 - DAY(@InputDate), @InputDate) 'EndDate'
	, DATEDIFF(DAY, DATEADD(MONTH, -3, DATEADD(DAY, 1 - DAY(@InputDate), @InputDate)), DATEADD(DAY, 1 - DAY(@InputDate), @InputDate)) 'Days'

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
C#.NET Programmer
 
Thanks so much for the quick response! This will work and I can tweak it from here!

Thanks again for your help!
 
Sounds like a good spot to use a calendar file.

Auguy
Sylvania/Toledo Ohio
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top