Postgres函数返回REGEXP_MATCHES结果
我正在尝试编写一些PostgreSQL功能,以帮助我解析诸如“ 30GI”或“ 25TI”之类的字符串值。我尝试过,但无法正确理解语法,也无法弄清楚第一个函数的返回类型应该是什么。
CREATE FUNCTION get_matches(VARCHAR) RETURNS ARRAY(VARCHAR) AS
SELECT (regexp_matches ($1, '(\d+)([KGTM]i)'));
CREATE FUNCTION get_amount(ARRAY(VARCHAR)) RETURNS VARCHAR AS
SELECT get_matches($1)[1];
CREATE FUNCTION get_units(ARRAY(VARCHAR)) RETURNS VARCHAR AS
SELECT get_matches($1)[2];
Postgres版本: postgresql 13.2在x86_64-pc-linux-gnu上,由GCC(Debian 8.3.0-6)8.3.0,64位
使用PGADMIN4测试我的功能。
I am trying to write some postgresql functions to help me parse a string value like "30Gi" or "25Ti". I tried this, but can't get the syntax right, and can't figure out what the return type should be for the first function.
CREATE FUNCTION get_matches(VARCHAR) RETURNS ARRAY(VARCHAR) AS
SELECT (regexp_matches ($1, '(\d+)([KGTM]i)'));
CREATE FUNCTION get_amount(ARRAY(VARCHAR)) RETURNS VARCHAR AS
SELECT get_matches($1)[1];
CREATE FUNCTION get_units(ARRAY(VARCHAR)) RETURNS VARCHAR AS
SELECT get_matches($1)[2];
Postgres version:PostgreSQL 13.2 on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
Testing my functions using pgAdmin4.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在一个查询中返回两个。以下返回2列大小的
表
,用于有效表达式。验证参数的正确格式(通过REGEX)后,它基本使用Regexp_replace
返回每个组件。如果需要,您可以在Select语句中使用结果。 (请参阅
You can get both returned in a single query. The following returns a
table
of 2 columns size and units for valid expressions. After validating the parameter is correctly formatted (via regex) it uses basically the same withregexp_replace
to return each component.You can use the results in a select statement if desired. (see demo).