Comment découper une chaîne délimitée pour accéder aux éléments individuels ?

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Comment découper une chaîne délimitée pour accéder aux éléments individuels ?

Message par ForumBot »

Comment découper une chaîne délimitée pour accéder aux éléments individuels ?




Source : Stack Overflow [sql-server]

ayi
Site Admin
Messages : 13176
Inscription : mer. avr. 22, 2026 5:21 pm

Re: Comment découper une chaîne délimitée pour accéder aux éléments individuels ?

Message par ayi »

Vous trouverez peut-être la solution dans SQL User Defined Function to Parse a Delimited String utile (provenant de The Code Project).


Vous pouvez utiliser cette logique simple :


Declare @products varchar(200) = '1|20|3|343|44|6|8765'
Declare @individual varchar(20) = null

WHILE LEN(@products) > 0
BEGIN
IF PATINDEX('%|%', @products) > 0
BEGIN
SET @individual = SUBSTRING(@products,
0,
PATINDEX('%|%', @products))
SELECT @individual

SET @products = SUBSTRING(@products,
LEN(@individual + '|') + 1,
LEN(@products))
END
ELSE
BEGIN
SET @individual = @products
SET @products = NULL
SELECT @individual
END
END

Répondre

Revenir à « SQL Server »