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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:48:33+00:00 2026-05-15T20:48:33+00:00

I have a sticky SQL issue and I’m not the best with SQL… I

  • 0

I have a sticky SQL issue and I’m not the best with SQL…

I have a table that has a varchar(40) column that has data in the following two formats:

nn-nnn1nnn00000
nn-nnn-nnn-0000

The second data type is outdated; however because they are outdated they need to be viewed first in order. It was recommended to me to substring the last 5 chars of the row, then cast it to a number and then perhaps, if the -0000 gets turned into a negative then the older ones will sort first. I’m not sure if this would work though…

The problem with sorting it normally is because as you can see in the new ones is a number in the 7th position. So when I try to sort I get:

nn-nnn-nnn-0001
nn-nnn-nnn-0002
nn-nnn-nnn-0003
nn-nnn1nnn00002 <-Should be 5th
nn-nnn2nnn00003 <-Should be 6th
nn-nnn3nnn00001 <-Should be 4th

Because this is for a asp .net page I was thinking about doing some crazy stuff with datatables but I know this would be better suited to be handled by SQL Server.

Is there a way to sort by the last five chars in a way where the older ones will show first while still allowing the new ones to sort next correctly?

I really don’t want to do some weird stuff in a datatable…

Thanks a million!!!

  • 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-15T20:48:34+00:00Added an answer on May 15, 2026 at 8:48 pm

    I would try with something like this (though it may be considered clunky):

    Select
      Col, Case Left(Right(Col, 5), 1) When '-' Then 0 Else 1 End As Old
    From
      Table
    Order By
      Old, Col
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL table that stores photos with a smallint SortOrder field. Users
I have a table that has a date, item, and quantity. I need a
We currently have a big project that consists of the following layers: MS SQL
Hi I have a Sticky form that after successful completion I want to clear
I have recently run into a particularly sticky issue regarding committing the result of
Have added the following code to my SQL query: (Note cut down version) DECLARE
SSIS (SQL 2005) packages that have a script task that invoke custom .NET dlls
I have the following partial methods in my partial class that is attached to
Using SQL Server 2008 R2 I'd like to create a table with the following
I have some predicates being dynamically built that have the following signature passes through

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.