Solved

Converting Excel formula to SQL Case II

Posted on 2014-03-10
5
230 Views
Last Modified: 2014-03-11
I would like to ask again for your help converting an excel formula to sql syntax.

the excel formula goes as follows:
Remaining Contract Length: 

=IF(SalesOrderTypeCode="Blanket", ContractEndDate-IF(TodaysDate > ContractStartDate,IF(TodaysDate>ContractEndDate, ContractEndDate, TodaysDate),ContractStartDate)+1, 0)

Open in new window


I got help originally to convert the formula, but more logic was added to it this I need more help.

I currently have the formula in sql like this:

,	RemainingContractLength =
	CASE 
	        WHEN SalesOrderLineTypeCode = 'BLANKET' 
		THEN DATEDIFF(dd, @Today, ContractEndDate) + 1 
		ELSE 0 
	END

Open in new window


Thank you very much for your help.
0
Comment
Question by:metropia
[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
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:c1nmo
ID: 39918357
You can nest, not tested but something like:

CASE
WHEN SalesOrderTypeCode="Blanket"
THEN DATEDIFF(dd, ContractEndDate,
      CASE WHEN TodaysDate > ContractStartDate THEN
            CASE WHEN TodaysDate>ContractEndDate THEN ContractEndDate ELSE TodaysDate END
      ELSE ContractStartDate END) + 1 ELSE 0 END
0
 

Author Comment

by:metropia
ID: 39918608
thank you.

somehow the number of days is not coming up the same when i run it on sql server.

i am attaching the excel file, if you notice, for row 13, the number on column Remaining Contract Length if 214, but in sql server comes back as 212

I am also attaching sample data from sql server
Sales-Order-Proration-Sample.xlsx
sample-data.txt
0
 
LVL 6

Expert Comment

by:c1nmo
ID: 39918727
I get -295 using your sample data with:

CASE
WHEN SalesOrderTypeCode='Blanket'
THEN DATEDIFF(dd, ContractEndDate,
      CASE WHEN GETDATE() > ContractStartDate THEN
            CASE WHEN GETDATE()>ContractEndDate THEN ContractEndDate ELSE GETDATE() END
      ELSE ContractStartDate END) + 1 ELSE 0 END
0
 
LVL 6

Accepted Solution

by:
c1nmo earned 500 total points
ID: 39918862
214 for row 13 values now:

select CASE
WHEN SalesOrderTypeCode='Blanket'
THEN DATEDIFF(dd,
      CASE WHEN Today > ContractStartDate THEN
            CASE WHEN Today>ContractEndDate THEN ContractEndDate ELSE Today END
      ELSE ContractStartDate END, ContractEndDate) + 1 ELSE 0 END  from test
0
 

Author Closing Comment

by:metropia
ID: 39921758
excellent solution!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

737 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