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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:36:53+00:00 2026-06-18T09:36:53+00:00

SELECT `01` FROM perf WHERE year = ‘2013’ order by CAST(`01` AS INT) LIMIT

  • 0
SELECT `01` FROM perf WHERE year = '2013' order by CAST(`01` AS INT) LIMIT 3

Column 01 has numeric values as varchar. I need to order top 3 of ’01’ as integer. Why doesn’t this query working?

Table like this;

+----------------------+
| name | 01 | 02 | year|
+----------------------+
|name1 | 90 |*** |2013 |
+----------------------+
|name2 | 93 | 55 |2013 |
+----------------------+
|name3 |*** | 78 |2013 |
+----------------------+

Query should order by 01 (dismiss *) and give names and values.

  • 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-06-18T09:36:55+00:00Added an answer on June 18, 2026 at 9:36 am

    MySQL doesn’t permit you to CAST('01' AS INT). It expects instead a SIGNED or UNSIGNED.

    SELECT `01` FROM perf WHERE year = '2013' order by CAST(`01` AS SIGNED) LIMIT 3
    

    Review the MySQL docs on CAST() for full details.

    mysql> SELECT CAST('01' AS SIGNED);
    +----------------------+
    | CAST('01' AS SIGNED) |
    +----------------------+
    |                    1 |
    +----------------------+
    1 row in set (0.00 sec)
    

    To force the non-numeric strings to be sorted last, you will need to apply a CASE in the ORDER BY which assigns them an absurdly high value. The condition should test that the value in 01 is not equal to 0, and when cast to a SIGNED the result is not 0, owing to the fact that non-numeric strings will cast to zero.

    If those conditions are not met, the string is assumed to be non-numeric, and given a value of 999999999 in the ORDER BY, which pushes them to the end. They’re subsequently ordered by name.

    SELECT * FROM perf 
    WHERE year = '2013'
    ORDER BY
      CASE WHEN (`01` <> '0' AND CAST(`01` AS SIGNED) <> 0) THEN CAST(`01` AS SIGNED) ELSE 999999999 END,
      name
    LIMIT 3
    

    http://sqlfiddle.com/#!2/846e2/6

    To make these sort descending, use an absurdly low value (negative) instead of a high value

      CASE WHEN (`01` <> '0' AND CAST(`01` AS SIGNED) <> 0) THEN CAST(`01` AS SIGNED) ELSE -999999999 END DESC,
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SELECT * FROM mydb ORDER BY score DESC LIMIT 5 it will display 5
Select * from tableName order by id desc limit 10 How to perform something
SELECT * FROM (SELECT ROW_NUMBER() over ( ORDER BY CASE WHEN @SortExpression ='Country_id' THEN
Is there any differences (perf) writting this request: Select * from T where PK
select * from ( select year, week, salesperson, count(*) as transactions, rank() over(partition by
SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY hrl.Frn) as Row, hrl.unq, hrl.LcnsId, hc.Business,hc.Name,hc.Phone,
SELECT * FROM CLIENT WHERE CLIENT_REG_DATE > DATEADD(year,-1,GETDATE()) GROUPBY MONTH(CLIENT_REG_DATE). I put into Apex
select * from sampleTable where CONVERT(VARCHAR(20),DateCreated,101) = CONVERT(VARCHAR(20),CAST('Feb 15 2012 7:00:00:000PM' AS DATETIME),101) I
SELECT * FROM users WHERE TO_SECONDS(NOW())-TO_SECONDS(TS) <= 604800 TS is timestamp column. 604800 is
SELECT * From `users` AS `User` LEFT JOIN `selections` AS `Selections` ON (`Selections`.`user_id` =

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.