我正在努力將某些東西從 Oracle 轉換為 PostgreSQL。在Oracle檔案中有一個函式:
instr(string,substring,starting point,nth location)
例子:
instr(text, '$', 1, 3)
在 PostgreSQL 中這不存在,所以我查找了一個等效函式(4 個引數很重要)。
我發現:strpos(str, sub)Postgres 中的功能相當于instr(str, sub)Oracle 中的功能。通過 split_part 嘗試了選項(沒有成功)。我只需要使用標準函式 Postgres(不是自己的函式)獲得相同的結果。也許有人會提供選項,甚至在代碼中是多余的。
uj5u.com熱心網友回復:
這可以在純 SQL 中使用string_to_array.
with tab(val) as (
select 'qwe$rty$123$456$78'
union all
select 'qwe$rty$123$'
union all
select '123$456$'
union all
select '123$456'
)
select
val
/*Oracle's signature: instr(string , substring [, position [, occurrence ] ])*/
, case
when
array_length(
string_to_array(substr(val /*string*/, 1 /*position*/), '$' /*substring*/),
1
) <= 3 /*occurrence*/
then 0
else
length(array_to_string((
string_to_array(substr(val /*string*/, 1 /*position*/), '$' /*substring*/)
)[:3/*occurrence*/],
'$'/*substring*/)
) 1
end as instr
from tab
| 值 | 指令 |
|---|---|
| qwe$rty$123$456$78 | 12 |
| qwe$rty$123$ | 12 |
| 123$456$ | 0 |
| 123$456 | 0 |
Postgres:小提琴
甲骨文:小提琴
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/522618.html
標籤:PostgreSQL指令
