Solved

DATEDIFF problem

Posted on 2013-06-28
3
408 Views
Last Modified: 2013-06-28
Hi,  I am trying to write a case statement were I am comparing to dates to determine the result.  I do not think I am using the datediff function correctly.  I am hoping someone can take a look at my code and offer suggestions.

select
case
          when
            DATEDIFF(day, reportdate) - DATEDIFF(day,[Expected Close Date]) <=182)
            then '6 months'
          when DATEDIFF(day, reportdate) - DATEDIFF(day,[Expected Close Date])<=365)
          then '12 months
          else 'Rest'
        end as timeframe
from dbo.wkly_report
0
Comment
Question by:knamc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39285492
This works for me in SSMS 2012

Declare @report date = '1-1-2012', @close date = '8-1-2012'

SELECT 
	CASE 
	WHEN DATEDIFF(d, @report, @close) <= 182 THEN '6 months'
	WHEN DATEDIFF(d, @report, @close) <= 365 THEN '12 months' 
	ELSE 'Rest' END as timeframe

Open in new window

0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39285525
Thanks for the grade.  Good luck with your project.  -Jim
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39286192
>>I do not think I am using the datediff function correctly
DATEDIFF ( datepart , startdate , enddate )

http://msdn.microsoft.com/en-us/library/ms189794(v=sql.105).aspx
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question