截取,查找,连接字符串sql

本文介绍了不同数据库系统如Oracle、MySQL、SQL Server和PostgreSQL中对字符串的截取、查找和连接的方法。包括从指定位置截取字符串、查找子串的位置、使用不同函数进行字符串连接以及计算字符串长度等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

oracle

从第3个字符开始,截取6个字符

select substr('Hello SQL!', 3, 6) from dual   -----llo SQ

查找子串在源串中的位置,从第5位开始,第二次出现

select instr('city_company_staff', '_', 5,2) from dual    -------13

字符串连接,oracle的concat()函数只支持两个参数,不支持两个以上字符串的拼接,可以使用多个concat()函数嵌套使用

select 'hello'|| 'world' || '你好' from dual  -----helloworld你好
select concat('hello', 'world' ) from dual  -----helloworld

字符串长度,lengthb返回字符串的长度,单位是字节

 select length('hello world你好') from dual   ----13
 select lengthb('hello world你好') from dual ----17

mysql:
从左边截取length

select left('city_company_staff',2)   ----ci

从右边截取length

select right('city_company_staff',2)     -----ff

从index开始,截取len长度,两个函数都支持

select substring('city_company_staff',2,5)   ------ity_c
select substr('city_company_staff',2,5)      ------ity_c

subdate(date,day)截取时间,时间减去后面的day

select subdate('2019-04-27',1)     ------2019-04-26

查找子串在源串的位置, locate第三个参数为开始查找的位置

select locate('Hello','Hello World!')     ---1
select position('Hello' in 'Hello World!')     ---1
select locate('Hello' , 'Hello World!',1)    ---1

字符串连接

select concat( 'hello', 'world' ,'www')  -----helloworldwww
select concat_ws('-', 'hello', 'world' ,'www')   -----hello-world-www

字符串长度,在mysql中length是计算字段的长度,一个汉字是算三个字符,一个数字或字母算一个字符

 select length('hello world你好')   ----17
 select char_length('hello world你好') ----13

sqlserver:
从左边截取length

select left('SqlServer_2008',3)   ----Sql

从右边截取length

select right('SqlServer_2008',3)    -----008

从index开始,截取len长度

select substring('SqlServer_2008',3,6)   ------lServe

查找子串在源串的位置, patindex()必须要用通配符,否则查询不到子字符串,charindex 第三个参数为开始查找的位置

select patindex('%Hello%','Hello World!')     ---1
select charindex('Hello','Hello World!',1)     ---1

字符串连接

select 'hello'+ 'world'   -----helloworld

字符串长度

 select len('hello world你好')   ----13
 select datalength('hello world你好') ----15

postgresql:
从index开始,截取len长度

select substring('hello world' from 3  for 5)     ------llo w
select substring('hello world' , 3  , 5)    -------llo w

查找子串在源串的位置

select position('h' in 'hello world')        ------1
select strpos('hello world', 'h')            ------1

字符串连接

select 'hello' || 'world'    -----helloworld

字符串长度

select length('hello world')    ----11
select char_length('hello world')   ----11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值