我需要在一个专栏中得到每个名字的第二个单词,我做了关于substr和instr的研究,但是我认为我有一个逻辑错误,我就是不能指出它。
select substr(vendor_name,instr(vendor_name, ' ')+1) from vendors ORDER BY vendor_id asc;发布于 2016-12-09 00:23:51
请尝试以下查询:
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中存在第二个空格(以及第一个空格)。如果您希望在本专栏中使用复杂的数据,则可能需要使用正则表达式。
https://stackoverflow.com/questions/41051137
复制相似问题