lundi 24 avril 2017

If statement in SQL how to do it

I've got an if statement (below) which runs in Tableau. I need to turn this into SQl - I've tried lots of different case statements, but they all get errors.

Can anyone advise how I'd do this?

    IF LEN(REPLACE([postcode]," ","")) = 7 THEN LEFT(REPLACE([postcode]," ",""),4)+" "+RIGHT(REPLACE([postcode]," ",""),3)
ELSEIF LEN(REPLACE([postcode]," ","")) = 6 THEN LEFT(REPLACE([postcode]," ",""),3)+" "+RIGHT(REPLACE([postcode]," ",""),3)
ELSEIF LEN(REPLACE([postcode]," ","")) = 5 THEN LEFT(REPLACE([postcode]," ",""),2)+" "+RIGHT(REPLACE([postcode]," ",""),3)
END

Aucun commentaire:

Enregistrer un commentaire