Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Microsoft SQL Server: Programming FAQ

T-SQL Hints and Tips

Handling delimited strings in T-SQL by ghreddy
Posted: 10 Oct 01

There may be cases where you want to work with delimited strings in T-SQL.
Following two examples will cover most of the stuff that you will face while
working with strings.

Find the count of words in a comma delimited string:

declare @mystring varchar(200)
set @mystring="vb,asp,sqlserver,html"
select (len(@mystring)-len(replace(@mystring,',',''))+1)

Parsing the delimited string:

--variable i is for current and j for previous locations
DECLARE @mystring varchar(255), @myword varchar(50)
DECLARE @i int,@j int
SELECT @mystring = 'vb,asp,sqlserver,html'
SELECT @i = 0,@j = 0
IF SUBSTRING (@mystring, LEN (@mystring), 1) <> ','
   SELECT @mystring = @mystring + ','
SELECT @i = CHARINDEX (',', @mystring, @i + 1)
WHILE @i > 0
   SELECT @myword = SUBSTRING (@mystring, @j+1, (@i - @j) -1)
   SELECT @myword
   SELECT @j = @i
   SELECT @i = CHARINDEX (',' , @mystring, @i + 1)

Back to Microsoft SQL Server: Programming FAQ Index
Back to Microsoft SQL Server: Programming Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close