--JEU DE TEST create table NOMS (nom varchar(50) null) insert into noms select 'DUPONT, Henry' 7 insert into noms select 'Etienne, jacques, emile' 8 et 9 insert into noms select ', MARIE, Laure, TSAR' 1, 7 et 7 insert into noms select ', CLOE, ARLETTE, PIERRE, GROS,' 1, 6, 9, 8, 6 insert into noms select 'JEROME BARBE' select * from NOMS --ALGO DE RECHERCHE --CHAINE A TROUVER DECLARE @CharToFind varchar(1) SET @CharToFind =',' DECLARE @POSITION_NOM INT --position du curseur dans nom DECLARE @NB_MOTS int --nb mots trouves DECLARE @SUBNOM varchar(50) -- reste chaine a comparer DECLARE @NOM varchar(50) --VALEUR COLONNE NOM de la table NOMS DECLARE @POS_VIRGULE INT -- POSITION A LAQUELLE ON TROUVE LA PREMIERE OCCURENCE DE LA VIRGULE --creation du curseur sur la table DECLARE cur_Table cursor FOR SELECT NOM FROM NOMS --ouverture du curseur OPEN Cur_Table --Lecture ligne à ligne -- on positionne la valeur de la colonne NOM dans la variable locale @NOM fetch next from cur_Table into @NOM --DEBUT DE LA BOUCLE SUR LA TABLE WHILE (@@FETCH_STATUS = 0) BEGIN SET @POSITION_NOM = 1 SET @SUBNOM = @NOM SET @NB_MOTS = 0 --on boucle sur la chaine tant qu'on n'a pas atteind fin du nom WHILE (@POSITION_NOM <= DATALENGTH(@NOM)) BEGIN -- on initialise la chaine de caractre dans laquelle il faut rechercher la virgule SET @SUBNOM = SUBSTRING(@NOM, @POSITION_NOM, DATALENGTH(@NOM) - @POSITION_NOM +1) SELECT @POS_VIRGULE = CHARINDEX(@CharToFind, @SUBNOM) --on a trouver au moins un vigule IF @POS_VIRGULE >0 BEGIN -- on ne prend pas en compte si vigule en debut et fin de mot IF @POS_VIRGULE <> 1 and @POS_VIRGULE <> DATALENGTH(@SUBNOM) SET @NB_MOTS = @NB_MOTS + 1 --on deplace le curseur sur la chaine juste apres la vigule trouvee SET @POSITION_NOM = @POSITION_NOM + @POS_VIRGULE END ELSE --on force sortie boucle si pas ou plus de virgule à trouver SET @POSITION_NOM = DATALENGTH(@NOM) + 1 END --comme on compte le nombre de vigules, il faut ajouter 1 pour avoir le nombre de mots SET @NB_MOTS = @NB_MOTS + 1 --Affichage resultat SELECT @NOM, @NB_MOTS --passage à ligne suivante de la table fetch next from cur_Table into @NOM END --fermeture du curseur Close Cur_Table --Liberation du curseur DEALLOCATE Cur_Table