Sample如:‘insus’ --> 's','u','s','n','i'
转换字符串的字符成单个字符并用逗号分隔,这样可以在动态SQL中供SELECT动态调用至WHERE IN条件内。
ConvertINString
CREATE FUNCTION [dbo].[ConvertINString]
(
@String NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE @Result NVARCHAR(MAX) = N''
DECLARE @I INT = LEN(@String)
WHILE @I > 0
BEGIN
set @Result = @Result + N',' + '''' + SUBSTRING(@String, @I, 1) + ''''
SET @I = @I - 1
END
IF LEN(@Result) > 0
SET @Result = SUBSTRING(@Result,2,LEN(@Result)-1)
RETURN @Result
END
(
@String NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE @Result NVARCHAR(MAX) = N''
DECLARE @I INT = LEN(@String)
WHILE @I > 0
BEGIN
set @Result = @Result + N',' + '''' + SUBSTRING(@String, @I, 1) + ''''
SET @I = @I - 1
END
IF LEN(@Result) > 0
SET @Result = SUBSTRING(@Result,2,LEN(@Result)-1)
RETURN @Result
END