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 7173151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:45:50+00:00 2026-05-28T15:45:50+00:00

One of column (Say column1 in table Table1) stores data as follow Column1 test2;test1;test3

  • 0

One of column (Say column1 in table Table1) stores data as follow

Column1
test2;test1;test3
test21;test11;test32
--

I want to update this column so that data is stored in sorted form(after splitting the data by 😉 as follow

Column1
test1;test2;test3
test11;test21;test32
--

How to write query in a simplest form? I am using SQL Server 2005


Can I assume there is not a simple solution available using T-SQL then I can go with CLR function?

  • 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-05-28T15:45:51+00:00Added an answer on May 28, 2026 at 3:45 pm

    An example of how to do this in T-SQL in a set-based fashion. It assumes that the target table has a PK which uniquely identifies each row.
    Note that the sort is text-based. I have added a third line of test data to show this.

    DECLARE @t TABLE
    (id int ,column1 varchar(50))
    
    INSERT @t
    SELECT 1,'test2;test1;test3'
    UNION ALL SELECT 2,'test21;test11;test32'
    UNION ALL SELECT 3,'test222;test22;test2;test1;test3'
    
    DECLARE @chr_delim char(1)
    SET @chr_delim = ';'
    
    ;WITH splitCTE
    AS
    (
    SELECT  D.id
            ,SUBSTRING(s,n,CHARINDEX(@chr_delim, s + @chr_delim,n) -n) as ELEMENT
    FROM (SELECT id, column1 as s from @t) AS D
    JOIN (SELECT ROW_NUMBER() OVER (ORDER BY name) AS n
          FROM master..spt_values
          ) AS nums
    ON n <= LEN(s)
    AND SUBSTRING(@chr_delim + s,n,1) = @chr_delim
    )
    UPDATE t
    SET column1 = z.col1
    FROM @t AS t
    JOIN    (
                SELECT DISTINCT id , STUFF(d.x,1,1,'') AS col1
                FROM splitCTE AS r
                CROSS APPLY (SELECT ';' + ELEMENT 
                                     FROM splitCTE AS c 
                                     WHERE c.id = r.id 
                                     ORDER BY ELEMENT FOR XML PATH ('')
                                    ) d(x)
            ) AS z
    ON z.id = t.id      
    
    SELECT * FROM @t
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In SQL Server, I have a table where a column A stores some data.
by default I have one column in MySQL table to be NULL. I want
I have a table where one column has duplicate records but other columns are
I have an HTML table with one column being email addresses; spambot issues aside,
Say I have a table A and it has 5 columns (Column1, Column2.. Column5),
Let's say I have table with column 'URL' whrere I store urls like this
We have a SQL server 2008 and one of the tables, say table A
i have one table in my database say mytable, which contents request coming from
Lets say I have one row with three columns - some buttons on left
I need to have one column as the primary key and another to auto

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.