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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:51:38+00:00 2026-05-31T19:51:38+00:00

I’m reading TSQL code someone else wrote and find a somewhat weird syntax. It’s

  • 0

I’m reading TSQL code someone else wrote and find a somewhat weird syntax. It’s doing order by a string. I did some test and the following is the code. Anyone can help me to explain it? Thanks.

First Query

SELECT  *
FROM    dbo.Products

Result:

ProductID   ProductName                     SupplierID  CategoryID  QuantityPerUnit      UnitPrice             UnitsInStock UnitsOnOrder ReorderLevel Discontinued
----------- ------------------------------- ----------- ----------- -------------------- --------------------- ------------ ------------ ------------ ------------
1           Chai                            1           1           10 boxes x 20 bags   18.00                 39           0            10           0
2           Chang                           1           1           24 - 12 oz bottles   19.00                 17           40           25           0
3           Aniseed Syrup                   1           2           12 - 550 ml bottles  10.00                 13           70           25           0
4           Chef Anton's Cajun Seasoning    2           2           48 - 6 oz jars       22.00                 53           0            0            0
...

*/

Second query:

SELECT  *
FROM    dbo.Products
WHERE   ProductID < 10
ORDER BY '3';

Result:

Msg 408, Level 16, State 1, Line 1 A constant expression was
encountered in the ORDER BY list, position 1.

Third Query

SELECT  *
FROM    dbo.Products
WHERE   ProductID < 10
ORDER BY CASE WHEN SupplierID = 2 THEN '1'
              WHEN SupplierID = 1 THEN '2'
              ELSE '3'
         END;

Result:

ProductID   ProductName                              SupplierID  CategoryID  QuantityPerUnit      UnitPrice             UnitsInStock UnitsOnOrder ReorderLevel Discontinued
----------- ---------------------------------------- ----------- ----------- -------------------- --------------------- ------------ ------------ ------------ ------------
4           Chef Anton's Cajun Seasoning             2           2           48 - 6 oz jars       22.00                 53           0            0            0
5           Chef Anton's Gumbo Mix                   2           2           36 boxes             21.35                 0            0            0            1
1           Chai                                     1           1           10 boxes x 20 bags   18.00                 39           0            10           0
2           Chang                                    1           1           24 - 12 oz bottles   19.00                 17           40           25           0
3           Aniseed Syrup                            1           2           12 - 550 ml bottles  10.00                 13           70           25           0
6           Grandma's Boysenberry Spread             3           2           12 - 8 oz jars       25.00                 120          0            25           0
7           Uncle Bob's Organic Dried Pears          3           7           12 - 1 lb pkgs.      30.00                 15           0            10           0
8           Northwoods Cranberry Sauce               3           2           12 - 12 oz jars      40.00                 6            0            0            0
9           Mishi Kobe Niku                          4           6           18 - 500 g pkgs.     97.00                 29           0            0            1

(9 row(s) affected)
*/
  • 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-31T19:51:39+00:00Added an answer on May 31, 2026 at 7:51 pm

    “Order by” has to be able to translate each row into a value, then those values can be compared. “Order by ‘3’” doesn’t make any sense as a useful query, as it’s not using the row – hence the error message of ordering by a constant expression.

    “Order by (some expression returning a string)” makes perfect sense. I would personally have used numbers rather than strings, but fundamentally it’s still just ordering by a value.

    Would you have found it odd to see “order by ProductName”? That’s ordering by a string too.

    Hopefully that helps – it’s not really clear which bit was causing a problem though.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.