Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8905447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:23:47+00:00 2026-06-15T02:23:47+00:00

Possible Duplicate: T-SQL: Opposite to string concatenation – how to split string into multiple

  • 0

Possible Duplicate:
T-SQL: Opposite to string concatenation – how to split string into multiple records
Splitting variable length delimited string across multiple rows (SQL)

I have a database table that contains column data like this:

Data (field name)
1111,44,666,77
22,55,76,54
32,31,56

I realise this is a very poor design because it is not normalised (I didn’t design it – I inherited it). Is there a query that will return the data like this:

1111
44
666
77
22
55
76
54
32
31
56

I am use to using CHARINDEX and SUBSTRING, but I cannot think of a way of doing this as the number of elements in each cell (delimited by a comma) is unknown.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:23:49+00:00Added an answer on June 15, 2026 at 2:23 am

    You can use CTE to split the data:

    ;with cte (DataItem, Data) as
    (
      select cast(left(Data, charindex(',',Data+',')-1) as varchar(50)) DataItem,
             stuff(Data, 1, charindex(',',Data+','), '') Data
      from yourtable
      union all
      select cast(left(Data, charindex(',',Data+',')-1) as varchar(50)) DataItem,
        stuff(Data, 1, charindex(',',Data+','), '') Data
      from cte
      where Data > ''
    ) 
    select DataItem
    from cte
    

    See SQL Fiddle with Demo

    Result:

    | DATAITEM |
    ------------
    |     1111 |
    |       22 |
    |       32 |
    |       31 |
    |       56 |
    |       55 |
    |       76 |
    |       54 |
    |       44 |
    |      666 |
    |       77 |
    

    Or you can create a split function:

    create FUNCTION [dbo].[Split](@String varchar(MAX), @Delimiter char(1))       
    returns @temptable TABLE (items varchar(MAX))       
    as       
    begin      
        declare @idx int       
        declare @slice varchar(8000)       
    
        select @idx = 1       
            if len(@String)<1 or @String is null  return       
    
        while @idx!= 0       
        begin       
            set @idx = charindex(@Delimiter,@String)       
            if @idx!=0       
                set @slice = left(@String,@idx - 1)       
            else       
                set @slice = @String       
    
            if(len(@slice)>0)  
                insert into @temptable(Items) values(@slice)       
    
            set @String = right(@String,len(@String) - @idx)       
            if len(@String) = 0 break       
        end   
    return 
    end;
    

    Which you can use when you query and this will produce the same result:

    select s.items declaration
    from yourtable t1
    outer apply dbo.split(t1.data, ',') s
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? I
Possible Duplicate: Split string in SQL I have seen a couple of questions related
Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? I
Possible Duplicate: SQL Server Output Clause into a scalar variable DECLARE @id int INSERT
Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? SQL
Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? Script
Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? I
Possible Duplicate: sql to pick apart a string of a persons name and output
Possible Duplicate: SQL left join vs multiple tables on FROM line? SELECT messages.message_title, messages.message_content,
Possible Duplicate: T-SQL Group Rows Into Columns I have a table _data that is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.