sql - DB2 字段值替换
问题描述
我有一个 DB2 员工表,其中有一个名为“Name”的列 - varchar。它不是主键。'Space' 在这里也被认为是有效值。此列下的某些字段只有空间。
现在我需要一个查询,通过将“Space”替换为 NULL 或任何其他默认值(例如“NoName”)来获取此员工表中的所有值。
我有一个想法,是在 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',否则返回实际值
推荐阅读
- c++ - 使用 Open GL 无法正确加载 bmp 纹理
- pointers - OCAML 中的命令式指针类型数据结构
- php - 我应该如何为 php 中的每个问题提供一个答案进行测验?
- unity3d - Unity UI 未在脚本中将可交互性设置为 true
- arrays - Vue.js 创建选择器,显示来自选定数据集的数据
- r - 在 R 函数中模拟静态变量
- javascript - 在加载图像之前显示网页
- java - 使用 Spring Security 时,将 Privileges 推送到数据库而不是以编程方式定义它们的原因是什么?
- scala - 使用查询参数测试 Play 控制器
- c# - Xamarin.Forms 索引有时会出现超出范围的异常,但并非总是如此,我该如何解决这个问题?