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!

Date Range - Determine # days in date range for each month

Status
Not open for further replies.

jasonhuibers

Programmer
Sep 12, 2005
290
CA
I have a Date1 and a Date2. I need to break it down as the following expected outcome using SQL:

Date1 Date2
08/12/2012 10/7/2012

Result:
Month Num Days
Aug, 2012 19
Sept, 2012 30
Oct, 2012 7
 
So do you have a company date table?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
No we do not have a company date table - would just like to plug in Date1 and Date2...
 
Here is something to consider.
Code:
declare @d1 date = '1/15/2013', @d2 date = getdate();
-- After checking the two are not in the same month
SELECT DATEDIFF(day, @d1,DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@d1)+1,0))), 
    DATEDIFF(day, DATEADD(month,DATEDIFF(month, 0,GETDATE()),0), @d2)
I am not sure about the months between

djj
The Lord is my shepherd (Psalm 23) - I need someone to lead me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top