首页 > 解决方案 > 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。

标签: sqloracleoracle11g

解决方案


第三个参数是搜索的“位置”,而不是出现。见这里 - 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

推荐阅读