Solved

Converting Excel formula to SQL Case II

Posted on 2014-03-10
5
229 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
  • 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how the fundamental information of how to create a table.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

830 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