1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| CREATE FUNCTION [dbo].[fctSplitToInt](
@DelimitedText VARCHAR(MAX), -- liste CSV
@QuoteChar CHAR(1) -- caractère de délimitation
) RETURNS @Items TABLE (Item INTEGER)
/* -- Retourne la liste sous la forme d'une colonne de nombres
-- Nota bene : les items non déclarés (chaînes vides ou ne comprenant que le caractère d'espacement) sont ignorés
*/
BEGIN
DECLARE @Item VARCHAR(11)
WHILE CHARINDEX(@QuoteChar, @DelimitedText, 0) <> 0 BEGIN
SELECT @Item=SUBSTRING(@DelimitedText,1,CHARINDEX(@QuoteChar,@DelimitedText, 0)-1),
@DelimitedText=SUBSTRING(@DelimitedText,CHARINDEX(@QuoteChar,@DelimitedText, 0) + LEN(@QuoteChar), LEN(@DelimitedText))
IF LEN(RTRIM(@Item)) > 0
INSERT INTO @Items SELECT CONVERT(INTEGER, @Item)
END
-- Dernier item de la liste
IF LEN(RTRIM(@DelimitedText)) > 0
INSERT INTO @Items SELECT CONVERT(INTEGER, @DelimitedText)
RETURN
END |
Partager