1
我正在嘗試使用Google Big Query查找字符串中子字符串第二次出現的索引。Google Big Query中的第二個子字符串
例如,在字符串「challcha」,「CH」的第二occurence將是在6位
我明白,這可以在使用的Oracle CHARINDEX來實現。我正試圖在Google Big Query中實現這一點。
任何幫助表示讚賞!
我正在嘗試使用Google Big Query查找字符串中子字符串第二次出現的索引。Google Big Query中的第二個子字符串
例如,在字符串「challcha」,「CH」的第二occurence將是在6位
我明白,這可以在使用的Oracle CHARINDEX來實現。我正試圖在Google Big Query中實現這一點。
任何幫助表示讚賞!
對於BigQuery的純SQL String functions
SELECT test,
INSTR(test, 'ch') + 1 + INSTR(SUBSTR(test, INSTR(test, 'ch') + 2), 'ch') AS pos,
FROM
(SELECT 'challcha' AS test),
(SELECT 'chcha' AS test),
(SELECT 'chha' AS test)
WHERE
INSTR(SUBSTR(test, INSTR(test, 'ch') + 2), 'ch') > 0
注:INSTR是區分大小寫的,所以你可能希望把所有的下限或上限,如果您有混合的情況下
SELECT test, pos FROM JS(
(
SELECT test FROM
(SELECT 'challcha' AS test),
(SELECT 'chcha' AS test),
(SELECT 'chha' AS test)
) ,
test,
"[{name: 'test', type:'string'},
{name: 'pos', type:'integer'}
]
",
"function(r, emit) {
var search = 'ch';
var pos1 = r.test.indexOf(search) + 1;
var pos2 = r.test.indexOf(search, pos1) + 1;
if (pos1 * pos2 == 0) pos2 = 0
emit({test: r.test, pos: pos2});
}"
)
純的BigQuery Regular expression functions
SELECT test,
LENGTH(REGEXP_EXTRACT(test, r'(?i)(.*?)ch')) + 3 +
LENGTH(REGEXP_EXTRACT(REGEXP_EXTRACT(test, r'(?i)ch(.*)'), r'(?i)(.*?)ch')) AS len,
FROM
(SELECT 'ChallCha' AS test),
(SELECT 'abChallCha' AS test),
(SELECT 'chcha' AS test),
(SELECT 'chha' AS test)
哇感謝,幫了我很多! – user3447653
如果答案幫助您解決了問題並且您接受了答案,那麼您還應該考慮對其進行投票。有關http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work#5235中的http://stackoverflow.com/help/someone-answers和Upvote部分的詳情,請參閱 –