PostgreSQL Oracle 兼容性之 - substrb (基于字节的字符串截取)

标签

PostgreSQL , substrb , 字节截取


背景

Oracle的substrb函数,用于基于字节流的截取,需要考虑多字节字符串的编码问题,未截取完整字符,则不截取。

https://docs.oracle.com/cd/B12037_01/olap.101/b10339/x_stddev004.htm

substr则用于基于字符串的截取。

PostgreSQL也可以支持类似的功能。

orafce插件

安装orafce插件,里面包含了大量的oracle兼容函数。

https://pgxn.org/dist/orafce/

postgres=# \df *.*substrb*  
                               List of functions  
   Schema   |  Name   | Result data type |    Argument data types     |  Type    
------------+---------+------------------+----------------------------+--------  
 pg_catalog | substrb | varchar2         | varchar2, integer          | normal  
 pg_catalog | substrb | varchar2         | varchar2, integer, integer | normal  
(2 rows)  

实际上这部分代码在PostgreSQL中已经存在,只是没有创建SQL函数。

src/backend/utils/adt/varlena.c

/*  
 * bytea_substr()  
 * Return a substring starting at the specified position.  
 * Cloned from text_substr and modified as required.  
 *  
 * Input:  
 *      - string  
 *      - starting position (is one-based)  
 *      - string length (optional)  
 *  
 * If the starting position is zero or less, then return from the start of the string  
 * adjusting the length to be consistent with the "negative start" per SQL.  
 * If the length is less than zero, an ERROR is thrown. If no third argument  
 * (length) is provided, the length to the end of the string is assumed.  
 */  
Datum  
bytea_substr(PG_FUNCTION_ARGS)  
{  
        PG_RETURN_BYTEA_P(bytea_substring(PG_GETARG_DATUM(0),  
                                                                          PG_GETARG_INT32(1),  
                                                                          PG_GETARG_INT32(2),  
                                                                          false));  
}  
  
  
static bytea *  
bytea_substring(Datum str,  
                                int S,  
                                int L,  
                                bool length_not_specified)  
{  
        int                     S1;                             /* adjusted start position */  
        int                     L1;                             /* adjusted substring length */  
  
        S1 = Max(S, 1);  
  
        if (length_not_specified)  
        {  
                /*  
                 * Not passed a length - DatumGetByteaPSlice() grabs everything to the  
                 * end of the string if we pass it a negative value for length.  
                 */  
                L1 = -1;  
        }  
        else  
        {  
                /* end position */  
                int                     E = S + L;  
  
                /*  
                 * A negative value for L is the only way for the end position to be  
                 * before the start. SQL99 says to throw an error.  
                 */  
                if (E < S)  
                        ereport(ERROR,  
                                        (errcode(ERRCODE_SUBSTRING_ERROR),  
                                         errmsg("negative substring length not allowed")));  
  
                /*  
                 * A zero or negative value for the end position can happen if the  
                 * start was negative or one. SQL99 says to return a zero-length  
                 * string.  
                 */  
                if (E < 1)  
                        return PG_STR_GET_BYTEA("");  
  
                L1 = E - S1;  
        }  
  
        /*  
         * If the start position is past the end of the string, SQL99 says to  
         * return a zero-length string -- DatumGetByteaPSlice() will do that for  
         * us. Convert to zero-based starting position  
         */  
        return DatumGetByteaPSlice(str, S1 - 1, L1);  
}  
postgres=# select octet_length(public.substrb('nihao 中国 abc你好'::varchar,2,9));
 octet_length 
--------------
            9
(1 row)

postgres=# select public.substrb('nihao 中国 abc你好'::varchar,2,9);
 substrb 
---------
 ihao 中
(1 row)

postgres=# select public.substrb('nihao 中国 abc你好'::varchar,2,10);
 substrb 
---------
 ihao 中
(1 row)

postgres=# select public.substrb('nihao 中国 abc你好'::varchar,2,8);
 substrb 
---------
 ihao 中
(1 row)

postgres=# select public.substrb('nihao 中国 abc你好'::varchar,2,6);
 substrb 
---------
 ihao 
(1 row)

postgres=# select public.substrb('nihao 中国 abc你好'::varchar,2,7);
 substrb 
---------
 ihao 
(1 row)

postgres=# select public.substrb('nihao 中国 abc你好'::varchar,2,9);
 substrb 
---------
 ihao 中
(1 row)

postgres=# select public.substrb('nihao 中国 abc你好'::varchar,2,10);
 substrb 
---------
 ihao 中
(1 row)
### 回答1: Postgresql中可以使用split_part函数对字符串进行分割,具体用法如下: ``` split_part(string, delimiter, field) ``` 其中,string为待分割的字符串,delimiter为分隔符,field为需要获取的分割后的子串的位置(从1开始计数)。 例如,想要获取字符串"hello,world"中的第二个子串,可以使用如下语句: ``` SELECT split_part('hello,world', ',', 2); ``` 执行结果为"world"。 另外,如果你想要截取字符串的一部分,可以使用substring函数,具体用法如下: ``` substring(string from start for length) ``` 其中,string为待截取的字符串,start为截取的起始位置,length为截取的长度。 例如,想要截取字符串"hello,world"中的第一个子串,可以使用如下语句: ``` SELECT substring('hello,world' from 1 for 5); ``` 执行结果为"hello"。 ### 回答2: 在PostgreSQL中,可以使用split_part函数来分割字符串。 split_part函数需要三个参数:字符串、分隔符、分割的位置。 例如,我们有以下字符串:'Hello,World,This,is,PostgreSQL',我们想要按照逗号分隔字符串,我们可以使用如下的SQL语句进行分割: SELECT split_part('Hello,World,This,is,PostgreSQL', ',', 1) AS part1, split_part('Hello,World,This,is,PostgreSQL', ',', 2) AS part2, split_part('Hello,World,This,is,PostgreSQL', ',', 3) AS part3 执行上述SQL语句后,我们会得到以下结果: part1: 'Hello' part2: 'World' part3: 'This' 这样,我们就成功地将字符串按照逗号进行了分割。 需要注意的是,如果分割的位置超过了字符串的实际分割数量,则会返回空字符串。 除了split_part函数外,PostgreSQL还提供了其他函数,如substring、strpos等,用于字符串的截取和查找。根据实际需求,可以选择合适的函数来处理字符串。 ### 回答3: PostgreSQL提供了多种方法来进行字符串分割和截取。 1. 使用split_part函数:split_part函数可以将一个字符串根据指定的分隔符进行分割,并返回指定位置的子字符串。其语法如下: split_part(string, delimiter, position) 例如,要截取字符串"Hello,World"中的"World",可以使用以下语句: SELECT split_part('Hello,World', ',', 2); 结果为"World"。 2. 使用substring函数:substring函数可以从一个字符串中截取指定位置和长度的子字符串。其语法如下: substring(string from start_position [for length]) 例如,要截取字符串"Hello,World"中的"World",可以使用以下语句: SELECT substring('Hello,World' from 7); 结果为"World"。 3. 使用regexp_split_to_table函数:regexp_split_to_table函数可以根据正则表达式将一个字符串分割成多个行,并以表格的形式返回。其语法如下: regexp_split_to_table(string, pattern) 例如,要将字符串"Hello,World"按逗号分隔并以表格形式返回,可以使用以下语句: SELECT regexp_split_to_table('Hello,World', ','); 结果为两行:"Hello"和"World"。 这些方法可以根据实际需求选择适合的方式进行字符串分割和截取。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值