x

DATEDIFF used for Month

I have to show when a record is due by the end of next month, then the following month and so on. I am using DATEDIFF below, but my code just gives me records due in less than 30 days from today. Could someone plese help me say, if due "month +1" and "month+2" etc.. I hope that makes sense...

,sum(CASE WHEN DateOpened Between @StartDate and @EndDate 
and DATEDIFF (Day, getdate(), DueDate) <30 then 1 else 0 end) as Post1
more ▼

asked Feb 17 '11 at 10:47 AM in Default

LtDan gravatar image

LtDan
305 29 35 37

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

You can still use datediff but specify month as the first parameter:

select
    sum(case when datediff(month, getDate(), DueDate) < 0 then 1 
        else 0 end) PastDue,
    sum(case when datediff(month, getDate(), DueDate) = 0 then 1 
        else 0 end) DueThisMonth,    
    sum(case when datediff(month, getDate(), DueDate) = 1 then 1 
        else 0 end) DueNextMonth,    
    sum(case when datediff(month, getDate(), DueDate) = 2 then 1 
        else 0 end) DueInTwoMonth
    from dbo.TheTable
    where DateOpened Between @StartDate and @EndDate;
Oleg
more ▼

answered Feb 17 '11 at 01:17 PM

Oleg gravatar image

Oleg
15.9k 2 4 24

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x977
x43

asked: Feb 17 '11 at 10:47 AM

Seen: 806 times

Last Updated: Feb 17 '11 at 10:50 AM