首页 > 解决方案 > DB2 字段值替换

问题描述

我有一个 DB2 员工表,其中有一个名为“Name”的列 - varchar。它不是主键。'Space' 在这里也被认为是有效值。此列下的某些字段只有空间。

现在我需要一个查询,通过将“Space”替换为 NULL 或任何其他默认值(例如“NoName”)来获取此员工表中的所有值。

我有一个想法,是在 SELECT 中使用 LTRIM 函数截断值,这将导致“空字符串”,如果我能找到一些返回 NULL 且以“空字符串”作为输入的字符串操作函数,我可以使用 COALESCE 的 IFNULL结果用 'NoName' 替换 NULL。

但我想不起任何这样的功能。你能帮我吗?或者有没有其他方法可以做到这一点?

标签: sqldatabasestringdb2

解决方案


你可以使用类似的东西:

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

当'space'或NULL或空字符串中的列时,它返回字符串'no data',否则返回实际值


推荐阅读