简体   繁体   中英

DB2 field value replacement

I have a DB2 employee table with a column called "Name" - varchar. it is not a primary key. 'Space' is also considered valid value here. some fields under this column has just space.

Now I need a query that fetches all the values in this employee table by replacing 'Space' with NULL or any other default value say 'NoName'.

One idea I have got, is to truncate the values with LTRIM function in SELECT, which will result 'empty string' and if I could find some string operation function that returns NULL with 'empty string' as input, I can use IFNULL of COALESCE to replace the NULL with 'NoName' as a result.

But I could not recall any such function. Can you help? Or is there any other way of accomplishing this ?

You can use something like:

Select case when <your column> = ' '    -- replace spaces
              or <your column> is null  -- replace nulls
              or <your column> = ''     -- replace empty strings
       then 'no data'                   -- with string 'no data'
       else <your column>               -- if 'regular name' returns it
       end as '<your column>'           -- gives the column a meaningful title
from yourtable

when the column in 'space' or NULL or empty string it returns the string 'no data' otherweise it returns the actual value

select name, coalesce(nullif(name, ''), 'NoName') name_new
from table (values ' ', null, '', 'a') t(name);

NAME NAME_NEW
---- --------
     NoName
-    NoName
     NoName
a    a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM