sql - 如何从postgres的文本列中获取第n行

标签 sql regex postgresql plpgsql stored-functions

如何创建函数

mline( value text, linenumber integer )

在 Postgres 9.1+ 中,它从文本列逐行返回。

例如,

create table test ( test text );
insert into test values ('Line 1
Line 2
Line 3');
select mline(test,2) from test;

应该返回

Line 2

使用 Postgres 9.1

最佳答案

您可以使用 regexp_split_to_array 和下标来获取特定元素:

create table test ( test text );
insert into test values ('Line 1
Line 2
Line 3');

SELECT (regexp_split_to_array(test, '\n'))[2] FROM test;

输出:

Line 2

db<>fiddle demo

关于sql - 如何从postgres的文本列中获取第n行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57506618/

相关文章:

sql - 在 Postgres 中插入自定义类型的数组

postgresql - Sqlalchemy:当池溢出时连接不会关闭

regex - postgres 9.1 中的模式匹配

c++ - 数据库连接可以重用吗?

iphone - 如何获取表中特定字段的最大值?

mysql - 如何从存储在 MySql 中的 URL 字符串中提取查询字符串值并将其保存在新字段中?

sql - 如何计算 MySQL 中的不同行数? (三栏为一体)

java - 使用 Java 将 GeoJSON 坐标中的纬度和经度值分别提取为所需的格式

javascript - 带空格的姓氏、名字的正则表达式

c++ - 使用 C++11 的 std::regex 填充子匹配时遇到问题