Solved

DATEDIFF problem

Posted on 2013-06-28
3
403 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
  • 2
3 Comments
 
LVL 65

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 65

Expert Comment

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

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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now