x
login about faq Site discussion (meta-askssc)

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 22 31 36

(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.4k 1 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.

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



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x912
x40

asked: Feb 17 '11 at 10:47 AM

Seen: 554 times

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

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.