拆分并替换SqlServer 2008中的字符串?

时间:2014-04-25 08:59:50

标签: sql-server split

我想拆分字符串' GPIN-KH2-COH-24042014-02' by' - '在sqlserver 2008中,并希望保存在单独的变量中。我怎样才能做到这一点?请帮助我。

现在我正在使用以下功能并通过它(' GPIN-KH2-COH-24042014-02',' - ')

Create FUNCTION [dbo].[fnSplit]
(
    @strInputList NVARCHAR (MAX),           -- List of Delimited Items
    @strDelimiter NVARCHAR (11) = ','       -- Delimiter that Separates Items
) 
RETURNS @tblList TABLE (strItem NVARCHAR(250))

BEGIN
    DECLARE @strItem NVARCHAR(MAX)

    WHILE CHARINDEX(@strDelimiter,@strInputList,0) <> 0
        BEGIN
            SELECT
                @strItem = RTRIM(LTRIM( (SUBSTRING (@strInputList, 1, CHARINDEX (@strDelimiter, @strInputList, 0) -1)))), 
                @strInputList = RTRIM(LTRIM( (SUBSTRING (@strInputList, CHARINDEX (@strDelimiter, @strInputList, 0) + LEN (@strDelimiter), LEN (@strInputList)))))

                IF LEN(@strItem) > 0
                    INSERT INTO @tblList SELECT @strItem
        END

        IF LEN(@strInputList) > 0
            INSERT INTO @tblList SELECT @strInputList 
    RETURN
END

结果:

strItem
GPIN
KH2
COH
24042014
02

当我是deimeter和string时,它返回该表格式的结果,如上所示。但我想得到最后两排。我怎么能得到这个?

3 个答案:

答案 0 :(得分:0)

使用此功能,从here

下载

使用

SELECT DBO.fnString_DelimeterIndex(N'GPIN-KH2-COH-24042014-02','-',1)
SELECT DBO.fnString_DelimeterIndex(N'GPIN-KH2-COH-24042014-02','-',2)
SELECT DBO.fnString_DelimeterIndex(N'GPIN-KH2-COH-24042014-02','-',3)

功能

ALTER FUNCTION [dbo].[fnString_DelimeterIndex]
(
    @Text NVARCHAR(4000),
    @Delimiter CHAR,
    @Section SMALLINT
)
RETURNS NVARCHAR(4000)
AS

BEGIN
    DECLARE @NextPos SMALLINT,
        @LastPos SMALLINT,
        @Found SMALLINT,
        @REVERSE BIT

    IF @Section < 0
        SELECT  @Text = REVERSE(@Text)--, @Section=1,@REVERSE=1

    SELECT  @NextPos = CHARINDEX(@Delimiter, @Text, 1),
        @LastPos = 0,
        @Found = 1

    WHILE @NextPos > 0 AND ABS(@Section) <> @Found
        SELECT  @LastPos = @NextPos,
            @NextPos = CHARINDEX(@Delimiter, @Text, @NextPos + 1),
            @Found = @Found + 1


    RETURN  CASE
            WHEN @Found <> ABS(@Section) OR @Section = 0 THEN NULL
            --WHEN @REVERSE =1 THEN 
            WHEN @Section > 0 THEN SUBSTRING(@Text, @LastPos + 1, CASE WHEN @NextPos = 0 THEN DATALENGTH(@Text) - @LastPos ELSE @NextPos - @LastPos - 1 END)
            ELSE REVERSE(SUBSTRING(@Text, @LastPos + 1, CASE WHEN @NextPos = 0 THEN DATALENGTH(@Text) - @LastPos ELSE @NextPos - @LastPos - 1 END))
        END
END

答案 1 :(得分:0)

CRETAE函数并将您的字符串和Delimiter作为参数传递

功能:

CREATE FUNCTION [dbo].[Split](@String varchar(8000), @Delimiter char(1))     
returns @temptable TABLE (items varchar(8000))     
as     
begin     
declare @idx int     
declare @slice varchar(8000)     

select @idx = 1     
if len(@String)<1 or @String is null  return     

while @idx!= 0     
begin     
   set @idx = charindex(@Delimiter,@String)     
   if @idx!=0     
       set @slice = left(@String,@idx - 1)     
   else     
     set @slice = @String     

   if(len(@slice)>0)
      insert into @temptable(Items) values(@slice)     

  set @String = right(@String,len(@String) - @idx)     
  if len(@String) = 0 break     
end 
return     
end

然后调用函数并根据需要使用值

DECLARE @A VARCHAR (100)= 'GPIN-KH2-COH-24042014-02'

SELECT items
INTO #STRINGS 
FROM dbo.split(@A,'-')

select * from #STRINGS

答案 2 :(得分:0)

这是解决问题的一种奇特方式:

你需要一个函数来先拆分它:

create function [dbo].[f_specialsplit]
(
@param nvarchar(max),
@delimiter char(1)
)
returns @t table (val nvarchar(max), rn varchar(9))
as
begin
set @param += @delimiter

;with a as
(
select cast(1 as bigint) f, charindex(@delimiter, @param) t where  @param is not null
union all
select t + 1, charindex(@delimiter, @param, t + 1)
from a
where charindex(@delimiter, @param, t + 1) > 0
)
insert @t
select substring(@param, f, t - f), row_number() over (order by (select 1)) from a
option (maxrecursion 0)
return
end

现在,Trick将数据转换为变量:

DECLARE @str varchar(100) = 'GPIN-KH2-COH-24042014-02'

DECLARE @s1 varchar(100),@s2 varchar(100),@s3 varchar(100),@s4 varchar(100),@s5 varchar(100)

SELECT @s1=[1],@s2=[2],@s3=[3],@s4=[4],@s5=[5] 
FROM f_specialsplit(@str,'-')
PIVOT
(min([val])  
FOR rn
in([1],[2],[3],[4],[5])  
)AS p

SELECT @s1,@s2,@s3,@s4,@s5

结果:

GPIN    KH2 COH 24042014    02