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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:05:31+00:00 2026-06-07T20:05:31+00:00

as the title states: I have a select query, which I’m trying to order

  • 0

as the title states:

I have a select query, which I’m trying to “order by” a field which contains numbers, the thing is this numbers are really strings starting with 0s, so the “order by” is doing this…

...
10
11
12
01
02
03
...

Any thoughts?

EDIT: if I do this: “…ORDER BY (field+1)” I can workaround this, because somehow the string is internally being converted to integer. Is this the a way to “officially” convert it like C’s atoi?

  • 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-07T20:05:33+00:00Added an answer on June 7, 2026 at 8:05 pm

    You can use CAST http://www.sqlite.org/lang_expr.html#castexpr to cast the expression to an Integer.

    sqlite> CREATE TABLE T (value VARCHAR(2));
    sqlite> INSERT INTO T (value) VALUES ('10');
    sqlite> INSERT INTO T (value) VALUES ('11');
    sqlite> INSERT INTO T (value) VALUES ('12');    
    sqlite> INSERT INTO T (value) VALUES ('01');
    sqlite> INSERT INTO T (value) VALUES ('02');
    sqlite> INSERT INTO T (value) VALUES ('03');
    sqlite> SELECT * FROM T ORDER BY CAST(value AS INTEGER);
    01
    02
    03
    10
    11
    12
    sqlite>
    

    if I do this: "…ORDER BY (field+1)" I can workaround this, because somehow the string is internally being converted to integer. Is the a way to "officially" convert it like C’s atoi?

    Well thats interesting, though I dont know how many DBMS support such an operation so I don’t recommend it just in case you ever need to use a different system that doesn’t support it, not to mention you are adding an extra operation, which can affect performance, though you also do this ORDER BY (field + 0) Im going to investigate the performance

    taken from the sqlite3 docs:

    A CAST expression is used to convert the value of to a different storage class in a similar way to the conversion that takes place when a column affinity is applied to a value. Application of a CAST expression is different to application of a column affinity, as with a CAST expression the storage class conversion is forced even if it is lossy and irrreversible.

    4.0 Operators
    All mathematical operators (+, -, *, /, %, <<, >>, &, and |) cast both operands to the NUMERIC storage class prior to being carried out. The cast is carried through even if it is lossy and irreversible. A NULL operand on a mathematical operator yields a NULL result. An operand on a mathematical operator that does not look in any way numeric and is not NULL is converted to 0 or 0.0.

    I was curios so I ran some benchmarks:

    >>> setup = """
    ... import sqlite3
    ... import timeit
    ... 
    ... conn = sqlite3.connect(':memory:')
    ... c = conn.cursor()
    ... c.execute('CREATE TABLE T (value int)')
    ... for index in range(4000000, 0, -1):
    ...     _ = c.execute('INSERT INTO T (value) VALUES (%i)' % index)
    ... conn.commit()
    ... """
    >>> 
    >>> cast_conv = "result = c.execute('SELECT * FROM T ORDER BY CAST(value AS INTEGER)')"
    >>> cast_affinity = "result = c.execute('SELECT * FROM T ORDER BY (value + 0)')"
    >>> timeit.Timer(cast_conv, setup).timeit(number = 1)
    18.145697116851807
    >>> timeit.Timer(cast_affinity, setup).timeit(number = 1)
    18.259973049163818
    >>>
    

    As we can see its a bit slower though not by much, interesting.

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

Sidebar

Related Questions

I have this sql query: SELECT S.SEARCH, S.STATUS, C.TITLE AS CategoryName, E.SEARCH_ENGINES AS Engine,
I have this query: SELECT p.id, r.status, r.title FROM page AS p INNER JOIN
As the title states, I have a C# console app which uses interop to
I have a query which I'm trying to group multiple table columns as text,
as the title states, I am trying to query multiple mySQL tables in the
I have a select query in which I have joined a couple of tables
As the title states, I am using devise. I came upon this link that
As the title states, I'm trying to resize a Texture2D before even considering SpriteBatch.Draw()
I have a table like this (MySQL 5.0.x, MyISAM): response{id, title, status, ...} (status:
I am working with the following SQL query: SELECT a.AppointmentId, a.Status, a.Type, a.Title, b.Days,

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.