Solved

DATEDIFF problem

Posted on 2013-06-28
3
405 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
execute a MS SQL script as a schedule SQL job 72 132
Order by but want it in specific order 2 34
Need help how to find where my error is in UFD 6 30
Sql Server group by 10 30
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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

832 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