Solved

Converting Excel formulas to SQL Case

Posted on 2014-03-06
14
968 Views
Last Modified: 2014-03-06
I would like to ask fro help converting a few excel formulas to sql syntax.

I have the task to use them in a stored procedure, but I am having difficulty converting them.

I have a sample of the excel file and the stored procedure I am using for testing, using hard coded values, same as the first row of the excel file.

There are 5 formulas in the following cells: D, J, K, L, M

Thank you very much for your help.
Sales-Order-Proration-Sample.xlsx
stored-procedure-sample.txt
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
  • 9
  • 4
14 Comments
 
LVL 12

Expert Comment

by:Harish Varghese
ID: 39909967
Can you please state the requirements (formula) in words?
0
 
LVL 34

Expert Comment

by:Norie
ID: 39909977
Where are the formulas you want to convert?
0
 

Author Comment

by:metropia
ID: 39910031
Formulas:

Number Of Days = IF(SalesOrderTypeCode="Blanket",IF(Today>ContractEndDate,1,IF(Today>ReportEndDate, 0,(IF(Today<ReportStartDate,(+C7ReportEndDate-ReportStartDate+1),(ReportEndDate-Today+1))))),(IF(OR(AND(RequestedDeliveryDate>=ReportStartDate,RequestedDeliveryDate<=ReportEndDate), AND(RequestedDeliveryDate>=Today,RequestedDeliveryDate<=ReportEndDate, Today>ReportStartDate)),1,0)))

Proration Quantity = +NumberOfDays * RemainingDailyQuantity

Original Contract Length 	= IF(SalesOrderTypeCode="Blanket",+ContractEndDate - ContractStartDate + 1, 0)

Remaining Contract Length = IF(SalesOrderTypeCode="Blanket",+ContractEndDate-Today+1,0)

Remaining Daily Quantity = IF(SalesOrderTypeCode="Standard",SalesOrderLineOpenQuantity,IF(Today>ContractEndDate,SalesOrderLineOpenQuantity,SalesOrderLineOpenQuantity/RemainingContractLength))

Open in new window

0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:metropia
ID: 39910034
The formulas are in the excel file I attached to this question.

The columns with formulas are: D, J, K, L, M
0
 

Author Comment

by:metropia
ID: 39910039
I attached the excel file that contains the columns with the formulas, and a stored procedure I started writing, using the values of row 7 in the excel file.
0
 

Author Comment

by:metropia
ID: 39910051
Thank you and sorry for the lack of clarity when typing my question
0
 
LVL 12

Accepted Solution

by:
Harish Varghese earned 500 total points
ID: 39910153
Here they are:
NumberOfDays = case when SalesOrderTypeCode='Blanket' then
	case when Today > ContractEndDate then 1
		when Today > ReportEndDate then 0
		When Today < ReportStartDate then ReportEndDate - ReportStartDate + 1,
		Else ReportEndDate - Today + 1
	End
Else
	case When (RequestedDeliveryDate >= ReportStartDate AND RequestedDeliveryDate <= ReportEndDate) 
				OR (RequestedDeliveryDate >= Today AND RequestedDeliveryDate <= ReportEndDate AND Today > ReportStartDate)
		Then 1,
	else 0 End
End

ProrationQuantity = NumberOfDays * RemainingDailyQuantity

OriginalContractLength = case when SalesOrderTypeCode = 'Blanket' then ContractEndDate - ContractStartDate + 1 Else 0 End

RemainingContractLength = case when SalesOrderTypeCode = 'Blanket' then ContractEndDate - Today + 1 else 0 end

RemainingDailyQuantity = 
	Case when SalesOrderTypeCode = 'Standard' Then SalesOrderLineOpenQuantity
		When Today > ContractEndDate Then SalesOrderLineOpenQuantity
		Else SalesOrderLineOpenQuantity/RemainingContractLength
	End
	

Open in new window

0
 

Author Comment

by:metropia
ID: 39910173
Wow. Thank you so much.

I tried using the first converted formula for "NumberOfDays" and I get an error:

Implicit conversion from data type datetime to int is not allowed. Use the CONVERT function to run this query.

Is it because the formula performs arithmetic with dates? would you be able to show me how to work around this problem?

Once again, thank you very much
0
 

Author Comment

by:metropia
ID: 39910188
the error comes from this part:

when @Today < @ReportStartDate 
then @ReportEndDate - @ReportStartDate + 1
else @ReportEndDate - @Today + 1

Open in new window

0
 
LVL 12

Assisted Solution

by:Harish Varghese
Harish Varghese earned 500 total points
ID: 39910214
Change the lines as below:
when @Today < @ReportStartDate 
then DATEDIFF(dd, @ReportStartDate, @ReportEndDate) + 1
else DATEDIFF(dd, @Today, @ReportEndDate) + 1

Open in new window

0
 

Author Comment

by:metropia
ID: 39910258
Thank you.

There is something that works different, because in excel I get a different number:

DECLARE @ReportStartDate DATETIME
DECLARE @ReportEndDate DATETIME
SET @ReportStartDate = '03/15/2014'      -- Excel Cell B
SET @ReportEndDate = '03/31/2014'      -- Excel Cell C

SELECT DATEDIFF(dd, @ReportEndDate, @ReportStartDate) + 1  

Gives me -15

But excel calculation gives me 17 as the number of days
0
 
LVL 12

Assisted Solution

by:Harish Varghese
Harish Varghese earned 500 total points
ID: 39910275
It should be
SELECT DATEDIFF (dd, @ReportStartDate, @ReportEndDate) + 1  
NOT SELECT DATEDIFF(dd, @ReportEndDate, @ReportStartDate) + 1
0
 

Author Comment

by:metropia
ID: 39910502
Harish,

Would the funtion DATEDIFF also be used in the subtraction formula for the column "RemainingContractLength"?

RemainingContractLength = case when SalesOrderTypeCode = 'Blanket' then ContractEndDate - Today + 1 else 0 end
0
 

Author Closing Comment

by:metropia
ID: 39910535
Impeccable work Harish. I really copied and pasted the code, made just a few changes and it worked smoothly.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
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…

726 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