sql - 我们如何在 Hadoop 中截断空格后的文本?
问题描述
我有一列说column_1
,它的值是:
abc 12edf
hbnm 847
47sf hg41
我需要如下输出:
abc
hbnm
47sf
PS:我对数据库有只读访问权限
解决方案
用于regexp_extract(col,'^(.*?)\\s',1)
提取正则表达式中空格(第 1 组)之前字符串开头的所有内容。
'^(.*?)\\s'
方法:
^
- 字符串锚点的开头
(.*?)
- 任意字符任意次数
\\s
- 空格
Demo:
with your_table as (--use your_table instead of this
select stack (3,
'abc 12edf',
'hbnm 847',
'47sf hg41'
) as str
)
select regexp_extract (str,'^(.*?)\\s',1) as result_str
from your_table s
结果:
abc
hbnm
47sf
另一种可能的解决方案是使用split
:
select split (str,' ')[0] as result_str
还有一种使用instr
+的解决方案substr
:
select substr(str,1,instr(str,' ')-1)
推荐阅读
- node.js - pm2 记录错误端口:3000 请修复此错误
- php - 此集合实例上不存在属性 [id]。Laravel 6 编辑页面
- python - 使用函数关闭 pygame 窗口
- if-statement - Flutter firebase 电子邮件/密码错误处理无法正确检查空白字段
- c - 远程客户端上的 gethostbyname 但有时会出现“资源暂时不可用”
- c - 为什么这段代码会创建正确的模式?
- .htaccess - 为什么在 wordpress htaccess http 到 https 重定向仅适用于主页?
- ios - 如何在 iOS Swift 中将数据数组回调到另一个 viewController
- git - 在 Github 上找不到存储库的设置
- python - Keras 导入模型 AttributeError:“NoneType”对象没有属性“op”