0

次の例のように、文字列を引数として受け入れる関数を作成しようとしています

this is a sample string and i need to use uppercase letter for every third word in this string

以下のような出力を取得する方法

position;word
-------------
3       ;"a"
6       ;"and"
9       ;"to"
12      ;"letter"
15      ;"third"
18      ;"this"

関数の作成方法は知っていますが、目的の出力を見つける方法が見つかりませんでした

create or replace function fn_every_third(str text) returns table(pos bigint,wrd text) as
$$
--code to get the output
$$
language sql
4

1 に答える 1

0
create or replace function fn_every_third(str text) returns table(pos bigint,wrd text) as
$$
with cte as(
select  * from(
select row_number() over() rn,word from(
select unnest(regexp_split_to_array(str,' ')) word)t)q
)
select rn ,word from cte 
where rn in (select generate_series(3,(select max(rn) from cte),3))
order by rn 
$$
language sql

使用する:

select * 
from fn_every_third('this is a sample string and i need to use uppercase letter for every third word in this string')

参照:

于 2015-12-24T07:24:43.807 に答える