当前位置:网站首页>数仓的字符截取三胞胎: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()的差异和用法,日常使用中,如果遇到截取字符串为多字节字符,或者截取参数可能为特殊值的情况,那你就要特别注意了;这篇文章,希望能帮到迷茫的你!
边栏推荐
- 数智学习|湖仓一体实践与探索
- Graduation summary
- Jenkins - 访问 Jenkins 自定义参数变量,处理变量值中含有空格
- 启牛开户安全吗?怎么线上开户?
- Wangxinling, tanweiwei Shanhai (extended version of Chorus) online audition lossless FLAC Download
- 《低代码解决方案》——覆盖工单、维修和财务全流程的数字化售后服务低代码解决方案
- SQL 注入绕过(四)
- TD Hero online conference on July 2
- Ionic4 realizes half star scoring
- 数智学习 | 流批一体实时数仓建设路径探索
猜你喜欢

文件傳輸協議--FTP

Embedded must learn, detailed explanation of hardware resource interface -- Based on arm am335x development board (Part 1)

Ti am3352/54/59 industrial core board hardware specification

Anonymous Mount & named mount

STM32的通用定时器与中断

MySQL优化小技巧

【历史上的今天】6 月 1 日:Napster 成立;MS-DOS 原作者出生;谷歌出售 Google SketchUp

Cesium obtains the latitude and longitude range of the screen

【历史上的今天】6 月 24 日:网易成立;首届消费电子展召开;世界上第一次网络直播

Domain Name System
随机推荐
Architecture high reliability application knowledge map ----- microservice architecture map
Snake C language
【历史上的今天】6 月 17 日:术语“超文本”的创造者出生;Novell 首席科学家诞生;探索频道开播
MySQL optimization tips
Cloud platform KVM migration local virtual machine records
How to handle computer security certificate errors
Figure out the difference between MIT, BSD and Apache open source protocols
Opencv——几何空间变换(仿射变换和投影变换)
Jenkins - 访问 Jenkins 自定义参数变量,处理变量值中含有空格
KVM related
The system administrator has set the system policy to prohibit this installation. Solution
A set of sai2 brushes is finally finished! Share with everyone!
Jenkins - access the Jenkins user-defined parameter variable, and handle the variable value containing spaces
Stm32f1 interrupt introduction
4G-learn from great partners
Prometheus 2.27.0 新特性
There appears to be a failure with your network connection Retrying.
Keil “st-link usb communication error“解决方法
Anonymous Mount & named mount
【历史上的今天】5 月 29 日:共享软件先驱诞生;ChromeBox 推出;VoodooPC 创始人出生