SQLTeam.com | Weblogs | Forums

Convert excel formula to SQL


#1

Hi, I have an script that need to incorporate this excel formula. Anybody can help me to transform this into SQL code like case statement. thank you in advance.

=IF(O14=0,0,IF(SUM(N14:O14)-(Q14+R14+V14)<0,0,SUM(N14:O14)-(Q14+R14+V14)-X14))


#2

You might want to break this into parts. I might suggest a CTE.


#3

There's no direct equivalent in SQL Server for an Excel range such as "N14:O14". Assuming you can get that value into a column named that, you can do something like this:

CASE WHEN O14 = 0 THEN 0
     WHEN [N14:O14] - (Q14+R14+V14) < 0 THEN 0
     ELSE [N14:O14] - (Q14+R14+V14) - X14
     END

#4

I assume that N14 and others is a column name. Thank you very much Scott.