Solved

Converting Excel formulas to SQL Case

Posted on 2014-03-06
14
853 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
  • 9
  • 4
14 Comments
 
LVL 12

Expert Comment

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

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
 

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Viewers will learn how the fundamental information of how to create a table.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

920 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

14 Experts available now in Live!

Get 1:1 Help Now