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

The Archive Base Latest Questions

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

Programs used: SQL Server 2000, Excel 2003 We have a table in our database

  • 0

Programs used:

SQL Server 2000, Excel 2003

We have a table in our database called Samples. Using the following query…

SELECT [Sample], [Flag] FROM Samples
ORDER BY [Sample]

… we get the following results:

 Sample    Flag
----------   ----
12-ABC-345      1
123-45-AB       0
679-ADC-12      1

When the user has the same data in an Excel spreadsheet, and sorts by the Sample column, they get the following sort order:

 Sample    Flag
----------   ----
123-45-AB       0
12-ABC-345      1
679-ADC-12      1

Out of curiosity, why is there a discrepancy between the sort in SQL and Excel (other than, “because it’s Microsoft”).

Is there a way in SQL to sort on the Sample column in the same method as the Excel method, or vice versa?

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

    The SQL server sorting is determined by the database, table, or field collation. By default, this is a standard lexicographical string sort (the character code for the hyphen is numerically lower than the character code for 1). Unfortunately, according to this Microsoft link, Excel ignores hyphens and apostrophes when sorting, except for tie-breaking. There’s no collation that does this specifically (that I’m aware of), so you’ll have to fake it.

    To achieve the same result in SQL Server, you’d need to do:

    SELECT [Sample], [Flag] FROM Samples
    ORDER BY REPLACE(REPLACE([Sample], '-', ''), '''', ''), 
        (CASE WHEN CHARINDEX([Sample], '-') > 0 THEN 1 ELSE 0 END) + 
        (CASE WHEN CHARINDEX([Sample], '''') > 0 THEN 1 ELSE 0 END) ASC
    

    This orders the results by the string as if it had all hyphens and apostrophe’s removed, then orders by a computed value that will yield 1 for any value that contains a hyphen or an apostrophe, or 2 for any value that contains both (and 0 for a value that contains neither). This expression will cause any value that contains a hyphen and/or apostrophe to sort after an expression that is otherwise equivalent, just like Excel.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQL Server 2000 and my ERP vendor has used a Text datatype
I have to import flat files into SQL Server. So far, I've used SSIS
I have a VB.NET application with a connection to an SQL Server 2003. On
I use the following stored procedure from my SQL Server 2008 database to return
Assume that you have a running SQL Server Express instance named (local)\SQLEXPRESS. Its database
I have designed database tables (normalised, on an MS SQL server) and created a
I have a Django application using django-mssql to communicate w/ SQL Server. This works
I'm trying to connect a local SQL Server database to an ETL utility called
I write programs used internally by our company. I've created several support DLLs that
Currently, changes to the database are made through the SQL Server Management program. IF

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.