OVH Cloud OVH Cloud

decuper une chaine

3 réponses
Avatar
Chris81
bonjour
j'ai un champ ou dedans j'ai R35 R23/R12 R45, je voudrais decouper
cette chaine suivant les espaces et l'inserer dans une autre table
comment puis je faire ?

je voudrais faire comme le split en vb.

merci

3 réponses

Avatar
msnews.microsoft.com
Le mieux serait que tu developpe ta propre fonction SPLIT en utilisant les
fonctions de traitement de chaines de T-SQL car il n'y, à ma connaissance,
rien de natif

Bien cordialement
Med Bouchenafa
"Chris81" wrote in message
news:
bonjour
j'ai un champ ou dedans j'ai R35 R23/R12 R45, je voudrais decouper cette
chaine suivant les espaces et l'inserer dans une autre table comment puis
je faire ?

je voudrais faire comme le split en vb.

merci




Avatar
Philippe T [MS]
Bonjour,

Un exemple d'utilisation :

INSERT INTO MaTable
SELECT MY_VALUE FROM dbo.cf_GetSplitTable('R35 R23/R12 R45', ' ')


Et la fonction :

CREATE FUNCTION dbo.cf_GetSplitTable
(
@sText nvarchar(4000),
@sDelim nvarchar(20) = ' '
)
RETURNS @MySplitTable TABLE (MY_VALUE nvarchar(4000) COLLATE
database_default, MY_ORDER int)
BEGIN

DECLARE @Val_Next_1 int
DECLARE @Val_Next_2 int
DECLARE @OccurenceNumber int

-- If the separator is not find
SET @Val_Next_1 = CHARINDEX(@sDelim, @sText)

IF (@Val_Next_1 = 0 OR @Val_Next_1 IS NULL)
BEGIN
IF @sText <> ''
BEGIN
INSERT INTO @MySplitTable( MY_VALUE, MY_ORDER )
VALUES( @sText, 1 )
END

RETURN
END

-- Insert the first occurence
SET @OccurenceNumber = 1

INSERT INTO @MySplitTable( MY_VALUE, MY_ORDER )
VALUES( SUBSTRING(@sText, 1, @Val_Next_1 - 1), @OccurenceNumber )

-- Insert all other occurence
WHILE ( 1 = 1 )
BEGIN
SET @Val_Next_1 = CHARINDEX(@sDelim, @sText)
SET @Val_Next_2 = CHARINDEX(@sDelim, SUBSTRING(@sText, @Val_Next_1 + 1,
4000))

SET @sText = SUBSTRING(@sText, @Val_Next_1 + 1, 4000)

IF @Val_Next_2 <> 0
BEGIN
SET @OccurenceNumber = @OccurenceNumber + 1

INSERT INTO @MySplitTable( MY_VALUE, MY_ORDER )
VALUES( SUBSTRING(@sText, 1, @Val_Next_2 - 1), @OccurenceNumber )
END
ELSE
BEGIN
IF @sText <> ''
BEGIN
SET @OccurenceNumber = @OccurenceNumber + 1

INSERT INTO @MySplitTable( MY_VALUE, MY_ORDER )
VALUES( @sText, @OccurenceNumber )
END
BREAK
END
END

RETURN
END












Phil.
________________________________________________________
Philippe TROTIN
Microsoft Services France http://www.microsoft.com/france
"msnews.microsoft.com" wrote in message
news:
Le mieux serait que tu developpe ta propre fonction SPLIT en utilisant les
fonctions de traitement de chaines de T-SQL car il n'y, à ma
connaissance, rien de natif

Bien cordialement
Med Bouchenafa
"Chris81" wrote in message
news:
bonjour
j'ai un champ ou dedans j'ai R35 R23/R12 R45, je voudrais decouper cette
chaine suivant les espaces et l'inserer dans une autre table comment puis
je faire ?

je voudrais faire comme le split en vb.

merci








Avatar
Chris81