Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2008
    Posts
    56

    Unanswered: split a column with commas between

    Hello,
    Can anyone help with tsql code (server 2005) to spit each character in a column with commas.

    example

    column1 varchar(10)

    column1
    mike

    need output to be
    m,i,k,e


    Thanks in advance

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    do that in your front-end application
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Jun 2004
    Location
    Long Island
    Posts
    696
    Quote Originally Posted by Rebecca Johnston
    Hello,
    Can anyone help with tsql code (server 2005) to spit each character in a column with commas.

    example

    column1 varchar(10)

    column1
    mike

    need output to be
    m,i,k,e


    Thanks in advance

    You can get the len of the col, while/loop for the len, and grab the substring(len,1) and append ',' within a while loop.

  4. #4
    Join Date
    Dec 2008
    Posts
    135
    declare @tab table (val varchar(12))
    declare @string nvarchar(255)
    set @string='Hello'
    declare @i tinyint
    set @i=1

    while @i < = len(@string)
    begin
    insert into @tab select substring(@string,@i,1)
    set @i=@i+1
    end

    select stuff((select ','+val from @tab for xml path('')),1,1,'')

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •