tsql字符串操作
把以前做项目时用到的字符串操作函数发出来:
/*-----------------------------------------------------------------------------------------
* Copyright(C) 2008 版权所有
* 功能描述:得到字符串长度
* 创建标识:亦心,2009年月日
* 修改标识:
-----------------------------------------------------------------------------------------*/
IF EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[dbo].Fun_GetStrArrayLength') AND type in (N'FN', N'PC'))
DROP FUNCTION [dbo].Fun_GetStrArrayLength
GO
CREATE function Fun_GetStrArrayLength
(
@str varchar(1024), --要分割的字符串
@split varchar(10) --分隔符号
)
returns int
as
begin
declare @location int
declare @start int
declare @length int
declare @len int
set @len=datalength(@split) --add
set @str=ltrim(rtrim(@str))
set @location=charindex(@split,@str)
set @length=1
while @location<>0
begin
set @start=@location+@len
set @location=charindex(@split,@str,@start)
set @length=@length+1
end
return @length
end
GO
/*-----------------------------------------------------------------------------------------
* Copyright(C) 2008 版权所有
* 功能描述:得到要索引的字符串
* 创建标识:亦心,2009年月日
* 修改标识:
-----------------------------------------------------------------------------------------*/
IF EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[dbo].Fun_GetStrArrayStrOfIndex') AND type in (N'FN', N'PC'))
DROP FUNCTION [dbo].Fun_GetStrArrayStrOfIndex
GO
CREATE function Fun_GetStrArrayStrOfIndex
(
@str varchar(1024), --要分割的字符串
@split varchar(10), --分隔符号
@index int --取第几个元素
)
returns varchar(1024)
as
begin
declare @location int
declare @start int
declare @next int
declare @seed int
set @str=ltrim(rtrim(@str))
set @start=1
set @next=1
set @seed=datalength(@split)
set @location=charindex(@split,@str)
while @location<>0 and @index>@next
begin
set @start=@location+@seed
set @location=charindex(@split,@str,@start)
set @next=@next+1
end
if @location =0 select @location =datalength(@str)+1
--这儿存在两种情况:、字符串不存在分隔符号2、字符串中存在分隔符号,跳出while循环后,@location为,那默认为字符串后边有一个分隔符号。
return substring(@str,@start,@location-@start)
end
GO
/*-----------------------------------------------------------------------------------------
* Copyright(C) 2008 版权所有
* 功能描述:得到字符串列表
* 创建标识:亦心,2009年月日
* 修改标识:
-----------------------------------------------------------------------------------------*/
if exists(select * FROM dbo.sysobjects WHERE id=object_id(N'dbo.Fun_SplitStr') AND type in (N'FN', N'PC'))
DROP FUNCTION Fun_SplitStr
GO
CREATE function Fun_SplitStr
(
@SourceSql varchar(8000),
@StrSeprate varchar(100))
returns @temp table(F1 varchar(100)
)
as
begin
declare @ch as varchar(100)
set @SourceSql=@SourceSql+@StrSeprate
while(@SourceSql<>'')
begin
set @ch=left(@SourceSql,charindex(@StrSeprate,@SourceSql,1)-1)
insert @temp values(@ch)
set @SourceSql=stuff(@SourceSql,1,charindex(@StrSeprate,@SourceSql,1),'')
end
return
END
GO
/*-----------------------------------------------------------------------------------------
* Copyright(C) 2008 版权所有
* 功能描述:合并处理函数
* 创建标识:亦心,2009年月日
* 修改标识:
-----------------------------------------------------------------------------------------*/
if exists(select * FROM dbo.sysobjects WHERE id=object_id(N'dbo.Fun_Join') AND type in (N'FN', N'PC'))
DROP FUNCTION Fun_Join
GO
CREATE FUNCTION dbo.Fun_Join(@col1 varchar(10))
RETURNS varchar(100)
AS
BEGIN
DECLARE @re varchar(100)
SET @re=''
SELECT @re=@re+','+CAST(col2 as varchar)
FROM tb
WHERE col1=@col1
RETURN(STUFF(@re,1,1,''))
END
GO
每天进步一点点...