把以前做项目用到的字符串操作函数发出来:
- /*------------------------------------------------------------------------------------------
- * Copyright(C) 2008 版权所有
- * 功能描述:得到字符串长度
- * 创建标识:亦心,2009年5月16日
- * 修改标识:
- ------------------------------------------------------------------------------------------*/
- 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=len(@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
- -----------------------------------------------------------------------------------------------------------------------------------
- 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
- /*------------------------------------------------------------------------------------------
- * Copyright(C) 2008 版权所有
- * 功能描述:得到要索引的字符串
- * 创建标识:亦心,2009年5月16日
- * 修改标识:
- ------------------------------------------------------------------------------------------*/
- 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=len(@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 =len(@str)+1
- --这儿存在两种情况:1、字符串不存在分隔符号 2、字符串中存在分隔符号,跳出while循环后,@location为0,那默认为字符串后边有一个分隔符号。
- return substring(@str,@start,@location-@start)
- end
- GO
- -----------------------------------------------------------------------------------------
- if exists(select * from dbo.sysobjects where id=object_id(N'dbo.Fun_SplitStr')
- and objectproperty(id,N'IsInlineFunction')=0)
- DROP FUNCTION Fun_SplitStr
- GO
- /*------------------------------------------------------------------------------------------
- * Copyright(C) 2008 版权所有
- * 功能描述:得到字符串列表
- * 创建标识:亦心,2009年5月16日
- * 修改标识:
- ------------------------------------------------------------------------------------------*/
- 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(',',@SourceSql,1)-1)
- insert @temp values(@ch)
- set @SourceSql=stuff(@SourceSql,1,charindex(',',@SourceSql,1),'')
- end
- return
- END
- GO
- /*------------------------------------------------------------------------------------------
- * Copyright(C) 2008 版权所有
- * 功能描述:合并处理函数
- * 创建标识:亦心,2009年5月16日
- * 修改标识:
- ------------------------------------------------------------------------------------------*/
- CREATE FUNCTION dbo.f_str(@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