欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SQL里面用自定義Split()完成個(gè)性化需求

 更新時(shí)間:2013年02月21日 15:06:14   作者:  
為了滿足需求自定義Split()在SQL中實(shí)現(xiàn),代碼很整潔,感興趣的朋友可以參考下,或許對(duì)你學(xué)習(xí)sql語(yǔ)句有所幫助
復(fù)制代碼 代碼如下:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [dbo].[SplitString]
(
@Input nvarchar(max),
@Separator nvarchar(max)=',',
@RemoveEmptyEntries bit=1
)
returns @TABLE table
(
[Id] int identity(1,1),
[Value] nvarchar(max)
)
as
begin
declare @Index int, @Entry nvarchar(max)
set @Index = charindex(@Separator,@Input)
while (@Index>0)
begin
set @Entry=ltrim(rtrim(substring(@Input, 1, @Index-1)))
if (@RemoveEmptyEntries=0) or (@RemoveEmptyEntries=1 and @Entry<>'')
begin
insert into @TABLE([Value]) Values(@Entry)
end
set @Input = substring(@Input, @Index+datalength(@Separator)/2, len(@Input))
set @Index = charindex(@Separator, @Input)
end
set @Entry=ltrim(rtrim(@Input))
if (@RemoveEmptyEntries=0) or (@RemoveEmptyEntries=1 and @Entry<>'')
begin
insert into @TABLE([Value]) Values(@Entry)
end
return
end

函數(shù)、表都建好了,下面調(diào)用測(cè)試一下吧:
復(fù)制代碼 代碼如下:

declare @str1 varchar(max), @str2 varchar(max), @str3 varchar(max)
set @str1 = '1,2,3'
set @str2 = '1###2###3'
set @str3 = '1###2###3###'
select [Value] from [dbo].[SplitString](@str1, ',', 1)
select [Value] from [dbo].[SplitString](@str2, '###', 1)
select [Value] from [dbo].[SplitString](@str3, '###', 0)

結(jié)果,截個(gè)圖來(lái)看一下:

相關(guān)文章

  • Sqlserver timestamp數(shù)據(jù)類(lèi)使用介紹

    Sqlserver timestamp數(shù)據(jù)類(lèi)使用介紹

    SQL Server timestamp 數(shù)據(jù)類(lèi)型與時(shí)間和日期無(wú)關(guān)。SQL Server timestamp 是二進(jìn)制數(shù)字,它表明數(shù)據(jù)庫(kù)中數(shù)據(jù)修改發(fā)生的相對(duì)順序。
    2011-08-08
  • sqlserver、mysql獲取連接字符串步驟

    sqlserver、mysql獲取連接字符串步驟

    這篇文章主要介紹了sqlserver、mysql獲取連接字符串步驟,需要的朋友可以參考下
    2014-07-07
  • 最新評(píng)論