1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
CREATE FUNCTION dbo.fn_DeleteAlphaInMyString( @mystring varchar(4000))
RETURNS int AS BEGIN
/*
*/
--Déclaration des vraiables
DECLARE @li_nb_char INT --Nombre de caractères dans la chaine
DECLARE @li_pos_current_char INT --Position du caractère courrant
DECLARE @ls_returned_string varchar(4000) -- Chaine retourné
DECLARE @ls_current_char CHAR(1) --Caractère courrant
--Initialisation des vraiables
SET @li_nb_char = len(@mystring)
SET @li_pos_current_char = 1
SET @ls_returned_string =''
--Si la chaine passé est vide on retourne la valeur null
IF @li_nb_char = 0 RETURN NULL
WHILE @li_pos_current_char <= @li_nb_char BEGIN
--On parcourt chaque caractère...
SET @ls_current_char = Substring(@mystring,@li_pos_current_char,1)
-- ... En vérifiant qu'il est numérique (compris entre 0 et 9)..
IF @ls_current_char BETWEEN '0' AND '9' BEGIN
--.. et dans ce cas on le rajoute à la chaine à retourner.
SET @ls_returned_string = @ls_returned_string + @ls_current_char
END
--On incrémente la boucle pour passer au caratère suivant
SET @li_pos_current_char = @li_pos_current_char + 1
END
-- On retourne la valeur numérique détecté
RETURN (SELECT convert(integer,@ls_returned_string))
END
go
--> Exemple pour tester la fonction
SELECT '123' AS DonneeAlphaNumerique
INTO #MATABLE
UNION
SELECT '4A56' AS DonneeAlphaNumerique
UNION
SELECT '78B%' AS DonneeAlphaNumerique
-->
SELECT * ,MonEntier=dbo.fn_DeleteAlphaInMyString (DonneeAlphaNumerique)
from #MATABLE |
Partager