Sql Server 中常用的字符串函数

本文介绍了SQLServer中常用的字符串函数,包括SubString用于截取字符串的一部分;Stuff用于替换字符串;CharIndex查找子串起始位置;Right和Left获取字符串左右两端的字符;Len计算字符串长度;Rtrim和Ltrim去除字符串尾部和前部空格;Upper和Lower转换字符串大小写;Replace替换字符串等。

Sql Server 中常用的字符串函数:

● SubString(string,starting position,length):返回字符串的一部分。

select substring(‘abcdefg’,3,2) as teststring

执行结果
teststring
——-
cd

● Stuff(string,insertion position,delete count,string inserted):substring函数相反。

select Stuff(‘abcdefg’,3,2,’123′) as teststring

执行结果
teststring
——-
ab123cdefg

● CharIndex(search string,string,starting postion):返回一个字符串在另一个字符串中的起始位置。

select CharIndex(‘c’,'abcdefg’,1) as teststring

执行结果
teststring
——-
3

● Right(string,count)和Left(string,count):返回指定字符串从最右边或者最左边开始指定个数的字符。

select Left(‘Nielsen’,2) as [Left],Right(‘Nielsen’,2) as [Right]

执行结果
LeftRight
—————–
Nien

● Len(string):返回指定字符串的长度。

select Len(‘abcdefg’) as teststring

执行结果
teststring
——-
7

● Rtrim(string)和Ltrim(string):删除起始或者尾随的空格。

select Rtrim(‘abcdefg’) as [Rtrim],Ltrim(‘abcdefg ‘) as [Ltrim]

执行结果
RtrimLtrim
——————–
**abcdefgabcdefg***代表空格,以便区别

● Upper(string)和Lower(string):将整个字符串转换为大写或者小写。

select Upper(‘abcd’) as [Upper],Lower(‘ABCD’) as [Lower]

执行结果
UpperLower
——————–
ABCD abcd

● Replace(source,search,replace):replace()函数在一个字符串中搜索指定的字符串,并用另一个字符串来替代它。

select Replace(‘abcdefg’,'abc’,'***’) as teststring

执行结果
teststring
——-
***defg

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值