当前位置:网站首页>数仓的字符截取三胞胎:substrb、substr、substring
数仓的字符截取三胞胎:substrb、substr、substring
2022-06-27 21:34:00 【华为云开发者联盟】
摘要:下面就来给大家介绍这三个函数在字符截取时的一些用法与区别。
本文分享自华为云社区《GaussDB(DWS)中的字符截取三胞胎》,作者:我站在北方的天空下 。
在GaussDB(DWS)中关于字符截取功能的支持有个函数三胞胎,它们分别是substrb()、substr()、substring(),很多人大概只知道它们可以操作字符串截取,再深入一点可能就不是很清楚了,有的是参数截取长度、有的参数是结束位置、有的参数可以是负数、有的不能接受负数参数·····
下面就来给大家介绍这三个函数在字符截取时的一些用法与区别吧。
概述
substr,substrb,substring均为字符串截取函数,都可带两个或三个参数,用于提取字符串中指定位置开始的指定长度的字符。 函数定义如下:
函数形式:
substrb(string, from [, count])
substr(string, from [, count])
substring(string, from [, count])
参数描述:
从参数string中抽取子字符串,from表示抽取的起始位置,count表示抽取的子字符串长度。
返回值类型:
text差异
1. 截取单位
substrb按字节截取,substr/substring按字符截取。以utf8编码为例,1个汉字占3个字节,当使用substrb截取长度3的子串时,只能截取到一个字符,而substr/substring可以截取到三个字符。

postgres=# select substrb('hwgs华为公司',3,5),substr('hwgs华为公司',3,5),substring('hwgs华为公司',3,5);
substrb | substr | substring
---------+----------+-----------
gs华 | gs华为公 | gs华为公
(1 row)2. 截取规则
GaussDB(DWS)目前支持三种兼容模式:ORA、TD和MySQL,分别对友商的函数行为进行兼容,提升用户迁移体验。在不同兼容模式下,函数差异表现为:
substrb(string, s[, n]):各兼容模式行为一致

postgres=# select substrb('hwgs华为公司',5,3),substrb('hwgs华为公司',8,3);
substrb | substrb
---------+---------
华 | 为
(1 row)
postgres=# select substrb('hwgs华为公司',-6,3),substrb('hwgs华为公司',-3,3);
substrb | substrb
---------+---------
公 | 司
(1 row)
postgres=# select substrb('hwgs华为公司',5,0),substrb('hwgs华为公司',8,-1);
substrb | substrb
---------+---------
|
(1 row)substr(string, s[, n]):s=0时存在兼容行为差异

postgres=# select substr('hwgs华为公司',5,3),substr('hwgs华为公司',8,3);
substr | substr
--------+--------
华为公 | 司
(1 row)
postgres=# select substr('hwgs华为公司',0,3),substr('hwgs华为公司',0,3);
substr | substr
--------+--------
hwg | hwg
(1 row)
mysql_db=# select substr('hwgs华为公司',0,3),substr('hwgs华为公司',0,3);
substr | substr
--------+--------
|
(1 row)substring(string, s[, n]):s<=0和n<0时存在兼容行为差异

postgres=# select substring('hwgs华为公司',0,3),substring('hwgs华为公司',-1,3);
substring | substring
-----------+-----------
hw | h
(1 row)
td_db=# select substring('hwgs华为公司',0,3),substring('hwgs华为公司',-1,3);
substring | substring
-----------+-----------
hw | h
(1 row)
mysql_db=# select substring('hwgs华为公司',0,3),substring('hwgs华为公司',-1,3);
substring | substring
-----------+-----------
| 司
(1 row)
td_db=# select substring('hwgs华为公司',0,-1);
ERROR: negative substring length not allowed
CONTEXT: referenced column: substring
mysql_db=# select substring('hwgs华为公司',0,-1);
substring
-----------
(1 row)小结
综上,详细介绍并总结了substrb()、substr()、substring()的差异和用法,日常使用中,如果遇到截取字符串为多字节字符,或者截取参数可能为特殊值的情况,那你就要特别注意了;这篇文章,希望能帮到迷茫的你!
边栏推荐
- 【AI应用】NVIDIA GeForce RTX 3060的详情参数
- 通过中金证券经理的开户二维码开股票账户安全吗?还是去证券公司开户安全?
- Although the TCGA database has 33 cancers
- 【PCL自学:PCLVisualizer】点云可视化工具PCLVisualizer
- 往前一步是优秀,退后一步是懵懂
- Zero foundation self-study SQL course | case function
- 【AI应用】Jetson Xavier NX的详情参数
- 十大券商注册账户安全吗,会有风险吗?
- ASP.NET仓库进销存ERP管理系统源码 ERP小程序源码
- C language - date formatting [easy to understand]
猜你喜欢

零基础自学SQL课程 | IF函数

VMware virtual machine bridging connectivity

一文剖析C语言函数

webService

halcon之区域:多种区域(Region)特征(6)
![[microservices sentinel] sentinel data persistence](/img/9f/2767945db99761bb35e2bb5434b44d.png)
[microservices sentinel] sentinel data persistence

vmware虚拟机桥接连通

Structure de stockage des graphiques

Storage structure of graph

Safe, fuel-efficient and environment-friendly camel AGM start stop battery is full of charm
随机推荐
An analysis of C language functions
[try to hack] kill evaluation
Vivado FFT IP的使用说明
Pat class B 1013
如何找到外文文献对应的中文文献?
Does the subscription of Siyuan notes stop deleting cloud data directly?
安全省油環保 駱駝AGM啟停電池魅力十足
Storage structure of graph
获取基因有效长度的N种方法
支持删除,更新任意结点的优先级队列
积分体系和营销活动结合在一起有哪些玩法
零基础自学SQL课程 | SQL基本函数大全
捷码赋能案例:湖南天辰产研实力迅速提升!实战玩转智慧楼宇/工地等项目
[PCL self study: pclplotter] pclplotter draws data analysis chart
C language character pointer and string initialization
[idea] idea formatting code skills
How to find Chinese documents corresponding to foreign documents?
How to solve the problem that the browser developed with CeF3 does not support flash
At the beginning of reading English literature, I would like to ask you how you should read it in the first place?
[sword finger offer] 48 Longest substring without duplicate characters