总觉得ms sql 字符串函数不够多,今天趁着一小段闲时时间编写了一个字符串插入指定字符串函数。闲话不多说直接贴代码。
--指定所以位置中插入指定字符串
create function fun_Insert(@txt nvarchar(2000),@index int,@chars nvarchar(2000))
returns nvarchar(4000)
as
begin
declare @len int
set @len=len(@txt)
if @index<1 begin
--插入第0位置
return @chars+@txt
end else if @index>=@len begin
--插入末尾位置
return @txt+@chars
end
--插入中间位置
return substring(@txt,1,@index)+@chars+substring(@txt,@index+1,@len-@index)
end
go
测试:
--插入最前端
select dbo.fun_Insert('指定所以位置中插入指定字符串',0,'http://www.naoqiu.com')
--插入末端
select dbo.fun_Insert('指定所以位置中插入指定字符串',100,'http://www.naoqiu.com')
--插入中间部分
select dbo.fun_Insert('指定所以位置中插入指定字符串',5,'http://www.naoqiu.com')
哪位博友有更多的字符串函数分享分享。
给自己网站打个小广告:福州公交查询网