use an if...else statement as a return value in sql

I'm trying to write his in sql:

SET `Field` = (
    If `Field` is empty then "b", 
    if `Field` is not empty, concat(`Field`,"_b"))

Meaning, if Field had value a it would change to a_b, if it was empty, it would change to b Is there an sql synthax that creates this if..else statement properly?

Answers


IF(Field = "" or Field IS NULL,'b',concat(Field,'_b'))

You can use a CASE statement.

Example:

SELECT 
    CASE  
        WHEN myField IS NULL THEN 'b'
        WHEN myField = '' THEN 'b'
        ELSE myField + '_b'
    END 
FROM myTable

You need to use the CASE keyword.

Something like

UPDATE tbl
SET field = 
    CASE
        WHEN field = ''
            THEN 'b'
            ELSE field + '_b'
    END

You can use Cases but you can shorten it like this:

 set Field = isnull(field, '') 
             +  case when isnull(field, '') = '' then '' else '_' end 
             + 'b'

Need Your Help

JVM meant to call main() and finalize() both, one is public and other is protected ? why?

java jvm main finalize

I was searching answer for finalize method made protected in object class, I came accross the answer and finally got confused as i found For finalize() method of object class it has been said that ...

Compiling sass with SassyStudio and Compass and multiple areas

sass compass-sass

Iam trying SassyStudio für Visual Studio to compile the SASS in our project - but there area some issues.