INSTR() 为不同的事件返回相同的值
INSTR() Returning Same Value For Different Occurrences
我有一个列存储了对一系列问题的所有回答,所有这些都在单个列中 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
当运行这个查询时,我可以通过instr
正确地得到'Do'
的第一次和第二次出现,但是出现3和4都是return相同的值作为事件 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
我有一个列存储了对一系列问题的所有回答,所有这些都在单个列中 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 |
当运行这个查询时,我可以通过instr
正确地得到'Do'
的第一次和第二次出现,但是出现3和4都是return相同的值作为事件 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