Solved

calculating dates

Posted on 2014-04-02
9
238 Views
Last Modified: 2014-04-03
I need to calculate a filed named: ContractOpenBeginDate

The concetp of the field is as follows:

ContractOpenBeginDate =  to Today's date if in the middle of contract (start & end dates) OR if contract has not started yet OR NULL if Contract is closed (contract end date is in the past)


The fields I will use are: Today (GETDATE()), ContractStartDate, ContractEndDate

I know this is a CASE statement, but I need help in writing the code if possible because I have not used these type of combinations before.

Any help would be great.

Thank you very much fro your help.
0
Comment
Question by:metropia
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
won't that logic always just return today's date?

 when would the ContractOpenBeginDate  not be today's date?
0
 

Author Comment

by:metropia
Comment Utility
sorry my bad.

use today if today falls in the middle of contract start and contract end date

use contract start date if that date is greater than today (future)

use null if contract is closed, or its end date is in the past.
0
 
LVL 40

Accepted Solution

by:
Sharath earned 350 total points
Comment Utility
CASE WHEN ContractStartDate <= CONVERT(DATE,GETDATE()) AND ContractEndDate >= CONVERT(DATE,GETDATE()) THEN CONVERT(DATE,GETDATE())  
     WHEN ContractStartDate > CONVERT(DATE,GETDATE()) THEN ContractStartDate
	 WHEN ContractEndDate < CONVERT(DATE,GETDATE()) THEN NULL END

Open in new window

0
 

Author Comment

by:metropia
Comment Utility
I came up with this statement but not sure if it is syntactically, logiaclly correct to the concept:

,    [ContractOpenStartDate] =
     (CASE
	WHEN @Today BETWEEN [vsol].[ContractStartDate] AND [vsol].[ContractEndDate]         
        THEN @Today
	ELSE
	    CASE
	        WHEN [vsol].[ContractStartDate] >  @Today THEN [vsol].[ContractStartDate]
		WHEN [vsol].[ContractEndDate] < @Today THEN NULL
	    END
    END)

Open in new window

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 39

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 50 total points
Comment Utility
your case statement looks fine as long as you set @today to be getdate. Sharath also posted a solution which works.

Note you don't need the else case . . . you can just continue the "whens"

 (CASE
      WHEN @Today BETWEEN [vsol].[ContractStartDate] AND [vsol].[ContractEndDate]        
        THEN @Today
                    WHEN [vsol].[ContractStartDate] >  @Today THEN [vsol].[ContractStartDate]
            WHEN [vsol].[ContractEndDate] < @Today THEN NULL
          END
)
0
 
LVL 31

Assisted Solution

by:awking00
awking00 earned 50 total points
Comment Utility
Be careful using between with dates. If ContractEndDate doesn't include a time value then, for example, 12-31-2014  would be considered to be 12-31-2014 and, if getdate() is 12-31-2014 10:30:44, it will not be considered to be between and return null. It is best to use where getdate() >= ContractBegnDate and get_date() < dateadd(day,1,ContractEndDate)
0
 
LVL 31

Expert Comment

by:awking00
Comment Utility
I meant to say "would be considered to be 12-31-2014 00:00:00" in the above comment.
0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 50 total points
Comment Utility
>>"Be careful using between with dates."
absolutely!

You will notice that in Sharath's suggestion he is converting getdate() to a date so that time within a day may be ignored. By using that approach you could use between.

Alternatively as awking00 has proposed you may continue to use an unconverted getdate() but then do not use between.

see: "Beware of Between"

So, if you adopt the conversion of getdate() to date for @Today then between is safe to use.

set @Today = convert(date, getdate() )

or

set @Today = dateadd(day, datediff(day,0, getDate() ), 0)


then the case expression
0
 

Author Closing Comment

by:metropia
Comment Utility
Thank you very much for the solution and all the good recommendations :)
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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

744 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

15 Experts available now in Live!

Get 1:1 Help Now