postgresql分割字符串,sql通过空格将字符串拆分为postgresql中的表

本文介绍了一个在PostgreSQL 8.2版本中缺失的regexp_split_to_table功能的替代方案,通过unnest和string_to_array函数组合实现简单字符串按空格拆分。适合处理如'hownowbrowncow'这样的输入,生成如'how', 'now', 'brown', 'cow'的表格形式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I looking for a function like regexp_split_to_table, but our db is version 8.2.9, so it doesn't have it. I'm really only splitting on a space, so a string like

how now brown cow

would return

+------+

|Column|

+------+

|how |

|now |

|brown |

|cow |

+------+

is there a simple function that can handle this, or something I have to write myself?

解决方案

You can split an array to a resultset by using the unnest function, and you can turn a string literal into an array by using the string_to_array function. Combine both and you get this:

alvherre=# select unnest(string_to_array('the quick lazy fox', ' '));

unnest

--------

the

quick

lazy

fox

(4 filas)

Since 8.2 does not have UNNEST, you can write it in PostgreSQL like this:

create or replace function unnest(anyarray) returns setof anyelement

language sql as $$

select $1[i] from generate_series(array_lower($1, 1),

array_upper($1, 1)) as i;

$$;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值