【问题标题】:How to find every third word from a string如何从字符串中找到每三个单词
【发布时间】:2015-12-24 07:20:58
【问题描述】:

我正在尝试创建一个接受字符串作为参数的函数,如下例所示

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

【问题讨论】:

    标签: sql postgresql string-function


    【解决方案1】:
    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')
    

    参考:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-11
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 2018-11-23
      相关资源
      最近更新 更多