Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to do Date Range by Months in SQL?

Author  Topic 

Maverick_
Posting Yak Master

107 Posts

Posted - 2013-04-16 : 07:29:49
Hi guys,

In my SELECT statement I am trying work out how to do the following categories but not sure how to do it:

The task is to classify enquiries by how many months they are open:

< Under a month
> Older than a month < 3 months
> Older than 3 months < 6 months
and
> Older than 6 months.

The two date fields to calculate the difference are enquiry_time and logged_date.

Anyone willing to share a code that does this?

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-04-16 : 07:50:18
You may need to calculate no of days in order to find required information for the three categories. For example
DateDiff(d,Logged_date,Enquiry_Time) between 30 and 89
DateDiff(d,Logged_date,Enquiry_Time) between 90 and 179
DateDiff(d,Logged_date,Enquiry_Time) >=180

Cheers
MIK
Go to Top of Page

Maverick_
Posting Yak Master

107 Posts

Posted - 2013-04-16 : 07:56:51
Cheers MIK I have applied a datediff accordingly as well.

Is there a way for SQL to look at calender months?
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-04-16 : 08:04:39
do you mean to find out month part of a specific date? Check out Month function. e.g. select MONTH(getdate())


Cheers
MIK
Go to Top of Page
   

- Advertisement -