sql - 在表格的每个字段和字段中的每个字符之间插入分隔符
问题描述
我有一个包含 4 个字段的表格 - 姓名、年龄、性别、城市。下面是样本数据。
我需要将输出作为单列获取,其中字段之间的值由“-”分隔,值内的字符由“-”分隔
输出:abcd-3-3-malepqr
我尝试了以下仅分隔字段的查询。
select trim(case when name is not null then name||'-'end ||case when age is not null then age||'-' end ||
case when sex is not null then sex||'-'end || case when city is not null then city end) from table
有没有办法在每个字段中分隔值。
我正在使用 Teradata 16,但如果在任何其他 RDBMS 中也可以使用,请提供帮助。
解决方案
首先连接所有列,然后使用正则表达式将破折号附加到每个字符
regexp_replace(coalesce(name,'')||
coalesce(trim(age),'')||
coalesce(sex,'')||
coalesce(city,'')
,'(?!^|$)' -- match every character besides begin and end of line
,'\1-') -- append dash to each match
推荐阅读
- python - 预测 sciklearn ValueError 的问题:操作数无法与形状一起广播
- python - python:object()不带参数
- emacs - 显示结果时在 CIDER REPL 中进行多余的滚动
- ssis - 如何将数据从数据库中的所有表传输到 azure 数据湖目的地?
- python - 如何在张量流中访问检测到的对象的名称?
- php - 与通过 Navicat 生成的查询计划相比,PHP 中的 Mysql 查询计划不同
- angular - Google Analytics(分析)未选择 gtag 自定义维度
- c# - 我无法定义“客户端”SignalR
- spring - org.hibernate.AssertionFailure:无法找到引用的实体映射
- django - 如何插入使用 CSS 而不使用输入标签本身?