[msSql]字符串插入指定字符串函數

總覺得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')

哪位博友有更多的字符串函數分享分享。


給自己網站打個小廣告:福州公交查詢網


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章