当前位置:网站首页>数仓的字符截取三胞胎:substrb、substr、substring
数仓的字符截取三胞胎:substrb、substr、substring
2022-06-28 00:50: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 allowedCONTEXT: referenced column: substringmysql_db=# select substring('hwgs华为公司',0,-1); substring-----------(1 row)小结
综上,详细介绍并总结了substrb()、substr()、substring()的差异和用法,日常使用中,如果遇到截取字符串为多字节字符,或者截取参数可能为特殊值的情况,那你就要特别注意了;这篇文章,希望能帮到迷茫的你!
边栏推荐
猜你喜欢

General timer and interrupt of stm32

Complex and inefficient logistics? Three steps to solve problems in enterprise administration

批阅2022春季学期课程小论文提交情况

Opencv——霍夫变换以及遇到的一些问题

如何系统学习LabVIEW?

后勤事务繁杂低效?三步骤解决企业行政管理难题

SQL 注入绕过(五)

关于st-link usb communication error的解决方法

Mysql大合集,你要内容的这里全都有

Skills in schematic merging
随机推荐
What is a web crawler
匿名挂载&具名挂载
Figure out the difference between MIT, BSD and Apache open source protocols
STM32F103的11个定时器
Résumé de la graduation
Locust 性能测试 - 参数化,并发循环数据采样不重复
JS random number (random number decimal)
王心凌、谭维维 - 山海(副歌加长版) 在线试听无损FLAC下载
ROS+Gazebo中红绿黄交通灯如何实现?
Jenkins - 内置变量访问
4G-learn from great partners
【历史上的今天】6 月 2 日:苹果推出了 Swift 编程语言;电信收购联通 C 网;OS X Yosemite 发布
【历史上的今天】6 月 24 日:网易成立;首届消费电子展召开;世界上第一次网络直播
Geojson format description (detailed format)
Machine learning (x) reinforcement learning
批阅2022春季学期课程小论文提交情况
数智学习|湖仓一体实践与探索
架构高可靠性应用知识图谱 ----- 架构演进之路
【历史上的今天】5 月 29 日:共享软件先驱诞生;ChromeBox 推出;VoodooPC 创始人出生
CVPR22收录论文|基于标签关系树的层级残差多粒度分类网络