Link to home
Start Free TrialLog in
Avatar of vdr1620
vdr1620Flag for United States of America

asked on

Convert Excel function to SQL

Need help converting this Excel nested If function to SQL script

=RIGHT(B1,LEN(B1)-IF(OR(MID(B1,1,1)="0",MID(B1,1,1)="9"),
IF(OR(MID(B1,2,1)="0",MID(B1,2,1)="1"),
IF(OR(MID(B1,3,1)="0",AND(MID(B1,3,1)="1",MID(B1,1,3)<>"001"),MID(B1,1,3)="900"),
IF(OR(MID(B1,4,1)="0",MID(B1,1,4)="9011",AND(MID(B1,1,5)="00021",LEFT(R95,2)="BR")),
IF(OR(MID(B1,5,1)="0",AND(MID(B1,1,5)="00021",LEFT(R95,2)="BR")),
IF(MID(B1,6,1)="0",6,5),4),3),2),1),0))

Ex: 00039019746598 should result in 39019746598

Appreciate your Help!!!!!!!
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Please state your requirements in a few sentences :)
vdr - your basic structure will be as follows.  From your previous question, I think you have a sense of how to write the SQL, you just needed the structure.  Your Excel expression has 6 conditions to test and that's reflected in the structure below.  Have a look and let me know us know if you need more detailed assistance.

IF ( Boolean_expression1 )
  BEGIN
    Sql Statement Block
  END
ELSE
  BEGIN
    IF ( Boolean_expression2 )
      BEGIN
        Sql Statement Block
      END
    ELSE
      BEGIN
        IF ( Boolean_expression3 )
          BEGIN
            Sql Statement Block
          END
        ELSE
          BEGIN
            IF ( Boolean_expression4 )
              BEGIN
                Sql Statement Block
              END
            ELSE
              BEGIN
                IF ( Boolean_expression5 )
                  BEGIN
                    Sql Statement Block
                  END
                ELSE
                  BEGIN
                    IF ( Boolean_expression6 )
                      BEGIN
                        Sql Statement Block
                      END
                    ELSE
                      BEGIN
                        Sql Statement Block
                      END
                  END
              END
           END            
      END
  END
To put this into context of your previous question:

Boolean Expression1 (BE1) is the equivalent of A: in the decision tree
Boolean Expression2 (BE2) = B:
BE3 = C:
BE4 = D:
BE5 = E:
BE6 = F:

Also, you'll need to use SUBSTRING() instead of MID():  MSDN SUBSTRING()
Rather than sifting through a bunch of excel functions, can you just state the criteria in English?
SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vdr1620

ASKER

Thanks again Mark for trying to put this together.. but i wanted to do this using CASE rather than IF as you will not be able to implement IF in an Inline statement

Thanks acperkins... this is exactly what i was looking for.. I will test it and let you know
No worries, vdr.  Hope ac's solution works for you.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vdr1620

ASKER

aikimark, I did try negating the conditions but ran into a snag and thought it would not work.. but you proved me wrong... Thanks
Avatar of vdr1620

ASKER

Thank you guys..