[SQL Server] Insérer une chaine de caractère dans une autre chaine de caractère

  • Imprimer

-- =============================================

-- Author:     Zouhaier KHARROUBI

-- Create date: 05/10/2021

-- Description: Insérer une chaine de caractère dans une autre chaine de caractère

-- =============================================

 

       -- Add the parameters for the function here

DECLARE             @Chaine                  AS NVARCHAR(MAX)

                    ,@ChaineInsert             AS NVARCHAR(MAX) -- La chaine à insérer

                    ,@IndiceDebutInsert        AS INT=0 -- L'indice de début de l'insertion

                    ,@IndiceFinInsert          AS INT=0 -- L'indice de la fin de l'insertion

                    ,@PasInsert                AS INT=2 -- Le pas d'insertion

                    ,@InsertDebut              AS BIT=1 -- Faire l'insertion au début de la chaine ou pas

                    ,@InsertFin                AS BIT=0 -- Faire l'insertion à la de la chaine ou pas

 

 

 

BEGIN

       -- Declare the return variable here

       DECLARE  @Taille AS INT

                    ,@Position AS INT

                    ,@Characetre AS CHAR(1)

                    ,@ChaineDebut AS NVARCHAR(MAX)

                    ,@ChaineFin AS NVARCHAR(MAX)

                    ,@Retour AS NVARCHAR(MAX)=@ChaineInsert

 

IF @PasInsert>0 AND @PasInsert<=LEN(@Chaine)

       BEGIN

 

             IF @IndiceDebutInsert>0

                    BEGIN

                          SELECT @Position=@IndiceDebutInsert

                          ,@ChaineDebut =SUBSTRING(@Chaine,1,@IndiceDebutInsert-1)

                    END

             ELSE

                    BEGIN

                          SELECT @Position=1,@ChaineDebut=''

                    END

 

             IF @IndiceFinInsert>0 AND @IndiceFinInsert<>LEN(@Chaine)

                    BEGIN

                          SELECT @Taille= @IndiceFinInsert,@ChaineFin=SUBSTRING(@Chaine,@IndiceFinInsert+1,LEN(@Chaine))

                    END

             ELSE

                    BEGIN

                          SELECT @Taille= LEN(@Chaine),@ChaineFin=''

                    END

 

             WHILE @Position<=@Taille

                    BEGIN

                          SELECT @Characetre=SUBSTRING(@Chaine,@Position,1)

                          SELECT @Retour = @Retour + @Characetre

 

                          IF @Position<>@Taille

                                 BEGIN

                                       IF @Position % @PasInsert = 0

                                              BEGIN

                                                    SELECT @Retour = @Retour + @ChaineInsert

                                              END

                                 END

 

                          SELECT @Position = @Position + 1

                    END

      

       IF @InsertDebut=1

             BEGIN

                    SELECT @Retour=@ChaineDebut + @Retour

             END

 

       IF @InsertFin=1

             BEGIN

                    SELECT @Retour=@Retour + @ChaineFin

             END

            

       END

ELSE

       BEGIN

             SELECT @Retour=@Chaine

       END

 

       -- Return the result of the function

       SELECT RTRIM(LTRIM(@Retour))

 

END