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

  • Home
  • SEARCH
  • 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 6090923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:16:09+00:00 2026-05-23T12:16:09+00:00

I have a table with data representing a tree structure, with one column indicating

  • 0

I have a table with data representing a tree structure, with one column indicating the row’s position in the hierarchical tree. Each level is separated with a -.

1
1-1
2
2-1
2-2
2-2-1
2-2-2
2-2-2-1

The tree is retrieved in order simply with an ORDER BY on this column. This falls down when there are more than 10 items at any level, as the column is sorted alphabetically. MySQL sorts 10 before 3.

Actual result:

1
1-10
1-3
2

Desired result:

1
1-3
1-10
2

There could be any number of levels of depth to the values.

Is it possible to sort this data numerically in MySQL?

  • 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-23T12:16:09+00:00Added an answer on May 23, 2026 at 12:16 pm

    I think your best shot is to convert the data into something that does naturally sort. If you tree structure will always have less than 99 children, you could create a function like I have below. You would just use the “GetTreeStructureSort(columnName)” in the sort function. (If you have the possibility of 3-digit numbers, you could adjust this to be more intuitive.)

    CREATE FUNCTION GetTreeStructureSort
    (
        -- Add the parameters for the function here
        @structure varchar(500) 
    )
    RETURNS varchar(500)
    AS
    BEGIN
    
        DECLARE @sort varchar(500)
    
        -- Add a hyphen to the beginning and end to make all the numbers from 1 to 9 easily replaceable
        SET @sort = '-' + @structure + '-'
    
        -- Replace each instance of a one-digit number to a two-digit representation
        SELECT @sort = REPLACE(@sort, '-1-', '-01-')
        SELECT @sort = REPLACE(@sort, '-2-', '-02-')
        SELECT @sort = REPLACE(@sort, '-3-', '-03-')
        SELECT @sort = REPLACE(@sort, '-4-', '-04-')
        SELECT @sort = REPLACE(@sort, '-5-', '-05-')
        SELECT @sort = REPLACE(@sort, '-6-', '-06-')
        SELECT @sort = REPLACE(@sort, '-7-', '-07-')
        SELECT @sort = REPLACE(@sort, '-8-', '-08-')
        SELECT @sort = REPLACE(@sort, '-9-', '-09-')
    
        -- Strip off the first and last hyphens that were added at the beginning.
        SELECT @sort = SUBSTRING(@sort, 2, LEN(@sort) - 2)
    
        -- Return the result of the function
        RETURN @sort
    
    END
    

    This would convert these results:

    1
    1-10
    1-3
    2
    

    into this:

    01
    01-03
    01-10
    02
    

    I tested this with the following code:

    DECLARE  @something varchar(255)
    set @something = '1-10-3-21'
    
    SELECT dbo.GetTreeStructureSort(@something)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data table containing one column as FilePath. FilePath D:\New folder\link.txt D:\New folder\SharepointMigration(Work
I want to create a table representing data I have, but I want each
Say I have a table of items representing a tree-like structured data, and I
I have a data structure inside a table in SQL Server 2005 representing a
I have a table of data that I need to dynamically add a column
I have some XML data representing a mathematical expression tree and want to convert
I have a view that displays a table: rows represent days - one row
Say that have the following CTE that returns the level of some tree data
I have table in data base name train delay, with columns train number(int), DelayTime(int),
I have a table of the form CREATE TABLE data { pk INT PRIMARY

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.