Solved

Convert Excel formula to SQL case statement

Posted on 2015-01-17
4
629 Views
Last Modified: 2015-01-27
Can someone help me convert the following Excel IF statement to either a SQL case statement or MS-Query SQL statement:

CurMonth

=IF(MONTH(getdate())=1,
IF(MO=12,
IF(YR=YEAR(getdate())-1,1,0),0),
IF(MO=MONTH(getdate())-1,
IF(YR=YEAR(getdate()),1,0)))


CurYear

=IF(MONTH(getdate())=1,
IF(MO<=12,
IF(YR=YEAR(getdate())-1,1,0),0),
IF(MO<=MONTH(getdate())-1,
IF(YR=YEAR(getdate()),1,0)))


PriorYear

=IF(MONTH(getdate())=1,
IF(MO<=12,
IF(YR=YEAR(getdate())-2,1,0),0),
IF(MO<=MONTH(getdate())-1,
IF(YR=YEAR(getdate())-1,1,0)))


Thanks

Glen
0
Comment
Question by:GPSPOW
  • 2
4 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 40555448
Hi Glen, the statements in your question look like a mixture of Excel and TSQL. Can you explain a bit more about what you'd like to achieve and post a few rows of sample data.
0
 

Author Comment

by:GPSPOW
ID: 40558770
I've requested that this question be deleted for the following reason:

Was able to convert Excel IF statement to a SQL query.

Thanks for reviewing it.

Glen
0
 

Accepted Solution

by:
GPSPOW earned 0 total points
ID: 40562562
Here is my solution to convertin the IF statement to a SQL case statement:

case when MONTH(getdate())=1 then
	case when MO=12 then
		  case when  YR = YEAR(GETDATE())-1 then 1 else 0 end
		else
	case when  MO = month(GETDATE()-1) and YR = YEAR(GETDATE()) then 1
		else 0 end end end as CMO,
	

Case when Month(getdate())=1 then
	case when MO <=12 then
		case when YR = year(getdate())-1 then 1 else 0 end
	else
	case when MO <=month(getdate()-1)  and YR=Year(getdate()) then 1 
		else 0 end end end as CYR,
	
Case when Month(getdate())=1 then
	case when MO <=12 then
		case when YR = year(getdate())-2 then 1 else 0 end
	else
	case when MO <=month(getdate()-1)  and YR=Year(getdate()-1) then 1 
		else 0 end end end as PYR

Open in new window



Thanks

Glen
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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

16 Experts available now in Live!

Get 1:1 Help Now