【问题标题】:Processing text array in PostgresSQL and convert to integer array在 PostgresQL 中处理文本数组并转换为整数数组
【发布时间】:2022-07-21 04:38:08
【问题描述】:

我在 Postgres 中有一个文本数组类型的列 - 示例 - {"Jun:2022,000/STD","May:2022,320/STD","Apr:2022,100/STD","Mar:2022,720/DDD","Feb:2022,000/STD"}

我想通过只保留“/”符号前的最后 3 位将其转换为整数数组

输出 - {000,320,100,720,000}

【问题讨论】:

  • 是postgres还是mysql?
  • select array_agg(substr(item, 10, 3)::int) from table cross join lateral unnest(column) as un(item);

标签: mysql sql arrays postgresql


【解决方案1】:

如果您的字符串格式与示例一致,那么@MikeOrganek 的解决方案是一个很好的解决方案。但是,如果格式不同,则需要使用正则表达式。也许是这样的:

with test( val ) as 
     ( select array['Jun:2022 xxx,000/STD','May:2022,320/STD','Apr:2022,100/STD','Mar:2022,720/DDD','Feb:2022,000/STD'] )
-- your query begins here
select array_agg(regexp_replace( item, '.*,.*(\d{3})\/.*$', '\1')::integer) "Integer Array"
  from  (select unnest(val) item from test) s

【讨论】:

    猜你喜欢
    • 2020-09-04
    • 1970-01-01
    • 2011-03-05
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-02
    相关资源
    最近更新 更多