我會建議在t-sql中使用字符串拆分功能將條目拆分爲更易於管理的信息。然後,使用類似:
if (select count(1)
from dbo.StringSplit('''item1'', ''item2'', ''item3''', ',')
where StringPart not like '''%''') > 0
...
這裏是我的personnal字符串分割功能,供大家參考:
create function [dbo].[StringSplit] (@p_String varchar(max), @p_Delimiter char(1) = ',')
returns @ReturnTable table (
[Index] int identity(1, 1) not null,
[StringPart] varchar(4000) not null
) as
begin
declare @EndPosition int
declare @StartPosition int = 0;
declare @TextPosition int = 1;
declare @ChunkLength smallint
declare @TemporaryString varchar(4000) = '';
declare @LeftOverString varchar(4000) = '';
declare @TemporaryValue varchar(4000) = '';
while @TextPosition <= Len(@p_String)
begin
set @ChunkLength = 4000 - Len(@LeftOverString)
set @TemporaryString = @LeftOverString + SubString(@p_String, @TextPosition, @ChunkLength)
set @TextPosition = @TextPosition + @ChunkLength
set @EndPosition = CharIndex(@p_Delimiter, @TemporaryString)
while @EndPosition > 0
begin
insert @ReturnTable values (LTrim(RTrim(SubString(@TemporaryString, @StartPosition + 1, @EndPosition - @StartPosition - 1))))
set @StartPosition = @EndPosition
set @EndPosition = CharIndex(@p_Delimiter, @TemporaryString, @StartPosition + 1)
end
set @LeftOverString = Right(@TemporaryString, Len(@TemporaryString) - @StartPosition)
end
insert @ReturnTable values (LTrim(RTrim(@LeftOverString)))
return
end
對不起,我花了一段時間來回答。非常感謝 - 這是一個很好的方法,非常感謝您的幫助。 – DanBennett 2013-02-26 15:10:36