sql - INSTR() 对不同的事件返回相同的值
问题描述
我有一列存储对一系列问题的所有答复,所有这些都在一个列responses
中。我不能将此列调整为单独的列,也不能调整此列的存储方式,只能在我的查询中操作该列。这是一个带有示例数据的 CTE 和我试图用来确定每个问题的开始的查询:
with t as (
select 'Do you have a cell phone with data plan: No
Do you have a cell phone with wifi capability: No
Do you have home internet access: Yes
Do you have public internet access: No' responses
from dual
union all
select 'Do you have a cell phone with data plan: Yes
Do you have a cell phone with wifi capability: Yes
Do you have home internet access: Yes
Do you have public internet access: Yes'
from dual
)
select instr(responses, 'Do', 1) o1
, instr(responses, 'Do', 2) o2
, instr(responses, 'Do', 3) o3
, instr(responses, 'Do', 4) o4
from t
结果:
o1 | o2 | o3 | o4 |
---|---|---|---|
1 | 45 | 45 | 45 |
1 | 46 | 46 | 46 |
运行此查询时,我可以正确获取'Do'
via的第一次和第二次出现instr
,但出现 3 和 4 都返回与出现 2 相同的值。我的假设是我使用instr
不正确,但我不确定如何正确格式化此以获得出现 3 和 4。
解决方案
第三个参数是搜索的“位置”,而不是出现。见这里 - https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions068.htm
您可以将 sql 修改为 -
select instr(responses, 'Do', 1, 1) o1
, instr(responses, 'Do', 1, 2) o2
, instr(responses, 'Do', 1, 3) o3
, instr(responses, 'Do', 1, 4) o4
from t
推荐阅读
- javascript - 渲染中的 ReactJs 键错误
- azure-active-directory - 如何使用 Azure Maps 进行谓词搜索,它只返回匹配的 Word 建议
- c++ - 如何使 Visual Studio 远程调试器 2017 和 2019 都在同一台计算机上工作?
- docker - Skaffold:AMD64 开发机器 (Mac),远程集群 ARM64 正在挑选 AMD64 映像
- security - 创建唯一的 802.11 身份验证方法
- sql - 如何使用 TABLE 运算符从两个表运行查询
- python - 无法解析从第三页开始的名称
- reactjs - ReacJS 不在 iOS 9 iPad 上渲染组件
- python - 在odoo中按模块更改产品图像
- ubuntu - 删除问候语中的 Fish shell 错误消息