[英]DB2 field value replacement
我有一个DB2员工表,其中包含一个名为“Name”的列 - varchar。 它不是主键。 “空间”在这里也被视为有效价值。 此列下的某些字段只有空格。
现在我需要一个查询,通过将'Space'替换为NULL或任何其他默认值'NoName'来获取此employee表中的所有值。
我得到的一个想法是在SELECT中使用LTRIM函数截断值,这将导致'空字符串',如果我能找到一些字符串操作函数,返回NULL并以'空字符串'作为输入,我可以使用COALESCE的IFNULL结果将'NoName'替换为NULL。
但我不记得任何这样的功能。 你能帮我吗? 或者还有其他方法可以实现这一目标吗?
您可以使用以下内容:
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'时,它返回实际值
select name, coalesce(nullif(name, ''), 'NoName') name_new
from table (values ' ', null, '', 'a') t(name);
NAME NAME_NEW
---- --------
NoName
- NoName
NoName
a a
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.