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?
IF(Field = "" or Field IS NULL,'b',concat(Field,'_b'))
You can use a CASE statement.
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.
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'