从 SQL Oracle 中的字符串中获取第二个单词
Fetching second word from a string in SQL Oracle
我需要获取列中每个名称的第二个单词,我研究了 substr 和 instr,但我认为我有一个逻辑错误,我根本无法指出。
select substr(vendor_name,instr(vendor_name, ' ')+1) from vendors ORDER BY vendor_id asc;
Here is my current output, the code works, but it does not do what I want it to do.
试试这个查询:
SELECT SUBSTR(vendor_name,
INSTR(vendor_name, ' ', 1, 1) + 1,
INSTR(vendor_name, ' ', 1, 2) - INSTR(vendor_name, ' ', 1, 1) - 1)
FROM vendors
ORDER BY vendor_id
要了解这是如何工作的,请举一个例子 vendor_name
of Boeing Corporation Inc.
在这种情况下:
INSTR(vendor_name, ' ', 1, 1) = 7 = first occurrence of space
INSTR(vendor_name, ' ', 1, 2) = 19 = second occurrence of space
现在这是我们对子串的调用:
SELECT SUBSTR('Boeing Corporation Inc.',
7 + 1,
19 - 7 - 1)
与
相同
SELECT SUBSTR('Boeing Corporation Inc.', 8, 11) = 'Corporation'
请注意,我的回答假设 vendor_name
中存在第二个 space(还有第一个 space)。如果您希望此列中的数据复杂,您可能需要改用正则表达式。
我需要获取列中每个名称的第二个单词,我研究了 substr 和 instr,但我认为我有一个逻辑错误,我根本无法指出。
select substr(vendor_name,instr(vendor_name, ' ')+1) from vendors ORDER BY vendor_id asc;
Here is my current output, the code works, but it does not do what I want it to do.
试试这个查询:
SELECT SUBSTR(vendor_name,
INSTR(vendor_name, ' ', 1, 1) + 1,
INSTR(vendor_name, ' ', 1, 2) - INSTR(vendor_name, ' ', 1, 1) - 1)
FROM vendors
ORDER BY vendor_id
要了解这是如何工作的,请举一个例子 vendor_name
of Boeing Corporation Inc.
在这种情况下:
INSTR(vendor_name, ' ', 1, 1) = 7 = first occurrence of space
INSTR(vendor_name, ' ', 1, 2) = 19 = second occurrence of space
现在这是我们对子串的调用:
SELECT SUBSTR('Boeing Corporation Inc.',
7 + 1,
19 - 7 - 1)
与
相同SELECT SUBSTR('Boeing Corporation Inc.', 8, 11) = 'Corporation'
请注意,我的回答假设 vendor_name
中存在第二个 space(还有第一个 space)。如果您希望此列中的数据复杂,您可能需要改用正则表达式。