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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:58:05+00:00 2026-05-10T22:58:05+00:00

I want to list (a sorted list) all my entries from an attribute called

  • 0

I want to list (a sorted list) all my entries from an attribute called streetNames in my table/relation Customers. eg. I want to achieve the following order:

Street_1A
Street_1B
Street_2A
Street_2B
Street_12A
Street_12B

A simple order by streetNames will do a lexical comparision and then Street_12A and B will come before Street_2A/B, and that is not correct. Is it possible to solve this by pure SQL?

  • 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. 2026-05-10T22:58:06+00:00Added an answer on May 10, 2026 at 10:58 pm

    The reliable way to do it (reliable in terms of ‘to sort your data correctly’, not ‘to solve your general problem’) is to split the data into street name and house number and sort both of them on their own. But this requires knowing where the house number starts. And this is the tricky part – making the assumption best fits your data.

    You should use something like the following to refactor your data and from now on store the house number in a separate field. All this string-juggling won’t perform too well when it comes to sorting large data sets.

    Assuming it is the last thing in the street name, and it contains a number:

    DECLARE @test TABLE (   street VARCHAR(100) )  INSERT INTO @test (street) VALUES('Street') INSERT INTO @test (street) VALUES('Street 1A') INSERT INTO @test (street) VALUES('Street1 12B') INSERT INTO @test (street) VALUES('Street 22A') INSERT INTO @test (street) VALUES('Street1 200B-8a') INSERT INTO @test (street) VALUES('') INSERT INTO @test (street) VALUES(NULL)  SELECT   street,   CASE      WHEN LEN(street) > 0 AND CHARINDEX(' ', REVERSE(street)) > 0     THEN CASE       WHEN RIGHT(street, CHARINDEX(' ', REVERSE(street)) - 1) LIKE '%[0-9]%'       THEN LEFT(street, LEN(street) - CHARINDEX(' ', REVERSE(street)))     END   END street_part,   CASE      WHEN LEN(street) > 0 AND CHARINDEX(' ', REVERSE(street)) > 0     THEN CASE        WHEN RIGHT(street, CHARINDEX(' ', REVERSE(street)) - 1) LIKE '%[0-9]%'       THEN RIGHT(street, CHARINDEX(' ', REVERSE(street)) - 1)     END   END house_part,   CASE      WHEN LEN(street) > 0 AND CHARINDEX(' ', REVERSE(street)) > 0     THEN CASE        WHEN RIGHT(street, CHARINDEX(' ', REVERSE(street)) - 1) LIKE '%[0-9]%'       THEN CASE         WHEN PATINDEX('%[a-z]%', LOWER(RIGHT(street, CHARINDEX(' ', REVERSE(street)) - 1))) > 0         THEN CONVERT(INT, LEFT(RIGHT(street, CHARINDEX(' ', REVERSE(street)) - 1), PATINDEX('%[^0-9]%', LOWER(RIGHT(street, CHARINDEX(' ', REVERSE(street)) - 1))) - 1))       END     END   END house_part_num FROM   @test  ORDER BY   street_part,   house_part_num,   house_part 

    This assumes these conditions:

    • a street address can have a house number
    • a house number must be the last thing in a street address (no ‘525 Monroe Av.’)
    • a house number should start with a digit to be sorted correctly
    • a house number can be a range (‘200-205’), this would be sorted below 200
    • a house number must not contain spaces or recognition fails (When you look at your data, you could apply something like REPLACE(street, ' - ', '-') to sanitize common patterns beforehand.)
    • the whole thing is still an approximation that certainly deviates from what it would look like in a telephone book, for example
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want a list of all the checked out files, by all users, in
I have an object, and I want to list all the selectors to which
Am working with django Publisher example, I want to list all the publishers in
If all tables I want to delete from have the column gamer_id can i
I want a list of hyperlinks on a basic html page, which point to
I want that list, because if something horrible happens, and I'll have to reinstall
I only want a list of files that have been added (not ones that
In a SharePoint list I want an auto number column that as I add
I want to clear the list of projects on the start page...how do I
I want to create a list of columns in SQL Server 2005 that have

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.