Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DATEDIFF problem

Posted on 2013-06-28
3
Medium Priority
?
412 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 2000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

636 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