Click here to Skip to main content
13,356,787 members (67,256 online)
Click here to Skip to main content
Add your own
alternative version

Stats

58.6K views
2 bookmarked
Posted 15 Mar 2012

SQL Server 2008 - Loop through/split a delimited string

, 15 Mar 2012
Rate this:
Please Sign up or sign in to vote.
Loop through/split a delimited string.
DECLARE @Str NVARCHAR(MAX)
SET @Str = ',Rajesh,Ganpat,Varu,Smith,GANUSHARMA,Vandana,Anil,Rajvir,Feroz,Anup,Manoj,'
DECLARE @Part NVARCHAR(MAX)
DECLARE @IND    INT
SET @IND = CHARINDEX(',',@Str)
DECLARE @EIND INT set @EIND = 0
WHILE(@IND != LEN(@STR))
BEGIN
    SET  @EIND = ISNULL(((CHARINDEX(',', @Str, @IND + 1)) - @IND - 1), 0)
    SELECT (SUBSTRING(@Str, (@IND  + 1),  @EIND))
    SELECT @IND = ISNULL(CHARINDEX(',', @STR, @IND + 1), 0)
END

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ganu Sharma
Software Developer Convexicon S/w solution.
India India
Current profile as asp.net & sql developer.

You may also be interested in...

Comments and Discussions

 
QuestionLoop through string and split a delimited string Pin
JERRY_M19-Nov-15 3:17
memberJERRY_M19-Nov-15 3:17 
SuggestionCan Use this... Pin
Deepak_pandey10-May-13 7:48
memberDeepak_pandey10-May-13 7:48 
QuestionOther solution Pin
Member 94816821-Jan-13 15:43
memberMember 94816821-Jan-13 15:43 
Bugbug Pin
Amiraka11-Sep-12 17:46
memberAmiraka11-Sep-12 17:46 
GeneralMessage Closed Pin
21-May-14 21:17
memberrba12321-May-14 21:17 
GeneralMy vote of 1 Pin
Amiraka11-Sep-12 17:45
memberAmiraka11-Sep-12 17:45 
Bug[My vote of 1] Problem Pin
Amiraka11-Sep-12 17:44
memberAmiraka11-Sep-12 17:44 
QuestionProblem Pin
Amiraka11-Sep-12 17:42
memberAmiraka11-Sep-12 17:42 
AnswerRe: Problem Pin
Me To Mee9-Oct-12 4:48
memberMe To Mee9-Oct-12 4:48 
Smile | :) Smile | :)
HI amiraka

it must be problems because it's XML Tag in SQL if you are proper make XML tag than it should be give error. lol..

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 15 Mar 2012
Article Copyright 2012 by Ganu Sharma
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid