У меня есть хранимая процедура SQLServer2008 R2, которая содержит алгоритм для анализа целых чисел из строки с разделителями.
Вот пример кода SQL, который я создал для циклического прохождения строки с разделителями и извлечения любых чисел, которыеможет существовать в строке с разделителями:
-- Create a delimited list for testing
DECLARE @NumericList nvarchar(MAX) = N'1, 33,44 ,55, foo ,666,77 77,8,bar,9,10'
-- Declare the delimiter
DECLARE @ListDelimiter VARCHAR(1) = ','
-- Remove white space from the list
SET @NumericList = REPLACE(@NumericList, ' ','');
-- Var that will hold the value of the delimited item during the while-loop
DECLARE @NumberInScope VARCHAR(MAX)
WHILE(LEN(@NumericList) > 0)
BEGIN
-- Get the value to the left of the first delimiter.
IF(CHARINDEX(@ListDelimiter, @NumericList) > 0)
SET @NumberInScope = LEFT(@NumericList, CHARINDEX(@ListDelimiter, @NumericList))
ELSE
SET @NumberInScope = @NumericList
-- Remove the @NumberInScope value from the @NumericList
SET @NumericList = RIGHT(@NumericList, LEN(@NumericList) - LEN(@NumberInScope))
-- Remove the delimiter from the @NumberInScope
SET @NumberInScope = REPLACE(@NumberInScope,@ListDelimiter,'')
-- Print only the integer values
IF(ISNUMERIC(@NumberInScope) = 1)
BEGIN
PRINT @NumberInScope
END
END
Приведенный выше код работает нормально, но после просмотра кода мне кажется, что должен быть более краткий способ сделать то же самое.Другими словами, есть ли какие-либо строковые функции (или любая новая функция R2, может быть), которую я упускаю из виду, что я могу реализовать, что уменьшило бы код и, надеюсь, было бы легче читать?