I have this sql query
SELECT `price` FROM `used_cars` ORDER BY `price` DESC
So I obviously want to order by price from high to low. However, it seems to be taking the first digit and sorting by that. My theory is that it is treating it like a string, and as the column is a varchar it makes sense. However, this is third party data, so I am stuck with it. How can I order so that the larger numbers come first?
So this is an example of how they are ordered
9698
8999
8988
8900
5983
4988
4984
42441
40949
3995
3995
38995
37685
36999
35983
34990
34785
32999
30594
29999
29999
2862
28000
27995
You should convert the column to a numeric data type. You can do that in the table definition, or in the query itself, for example with:
... ORDER BY `price`+0 DESC
CAST should work:
SELECT CAST(price AS UNSIGNED) AS NumPrice
FROM used_cars
ORDER BY NumPrice DESC
This should work:
(...)
ORDER BY CAST (price AS INT)
This will work but is bad performance
SELECT price FROM used_cars ORDER BY CAST(price AS int) DESC
Related
I'm trying to sort by a column called date which stores various dates in format MMYYYY (for example, 122020, 102019, etc.).
The SQL query that I have looks like this:
SELECT `date`, `invested` FROM `savings` WHERE `id` = 123 ORDER BY STR_TO_DATE(`date`, "%m%Y") ASC but this query does not sort the output correctly.
Any ideas on how to sort it properly?
Important note: Reclassifying/modifying the column's type is not an option at this point.
EDIT: My current SQL query, which is described above, sorts the dates like this: 102019, 102020, 112019, 112020. But my goal is to have it like this: 102019, 112019, 102020, 112020.
Thank you
Try separating the date string into two parts (year and month), cast each part as integer, and then ordering by that number:
SELECT
date_,
invested
FROM
savings
WHERE
id = 123
ORDER BY
CAST(SUBSTRING(date_, 3, 6) AS UNSIGNED),
CAST(SUBSTRING(date_, 1, 2) AS UNSIGNED)
;
This will do the job :) refer to this DB Fiddle for clarification.
I have a table with several rows of timestamp (unix epoch)
eg: 1620518277 , 1556748676 , 1547547076, 1602756807, 944971077 (field name -> date_stamp)
And by using
SELECT *
FROM table
ORDER BY date_stamp DESC
The result of this query is :
1. 944971077
2. 1620518277
3. 1602756807
4. 1556748676
5. 1547547076
Everything is sorted fine but how can 944971077 > 1620518277 ???
Anybody had this kind of strange SQL issues ?
Presumably, you are storing these timestamps as strings, not as numbers. A simple option forces a numeric conversion:
SELECT * FROM table ORDER BY date_stamp + 0 DESC
This would occur if timestamp were a string. A simple method is to convert to a number using implicit conversion:
SELECT *
FROM table
ORDER BY date_stamp + 0 DESC
I was having some trouble when trying to write an SQLstatement which sorted by time column which is in varchar format in ascending order. Here is my SQL statement:
SELECT mrtpopTime, mrtpopAmt
FROM tm_mrtpop
WHERE mrtpopName = ''
ORDER BY mrtpopTime
And I am getting these results:
As you can see from the picture, it was sorted by character by character. Is there anyway to sort it like:
0:00, 1:00, 2:00, 3:00 all the way to 23:00
Any ideas? Thanks in advance.
Convert varchar time column to a time (or dummy date plus time) and order by that:
SELECT mrtpopTime, mrtpopAmt
FROM tm_mrtpop
WHERE mrtpopName = ''
ORDER BY STR_TO_DATE(mrtpopTime, '%H:%i')
As the mrtpopTime is in varchar it won't sort. Hence cast it into float and order by
SELECT mrtpopTime, mrtpopAmt
FROM tm_mrtpop
WHERE mrtpopName = ''
ORDER BY 'cast(mrtpopTime as float) time'
I need to sort a table by date (descending), but all columns in the table are varchar, so I need to manipulate the data on the fly for sorting it correctly.
date sales
10/09/2014 100
13/09/2014 250
30/08/2014 200
Is that possible without altering the table? So the result will be like below, newest dates first?
date sales
13/09/2014 250
10/09/2014 100
30/08/2014 200
Like pseudocode
SELECT * FROM table ORDER BY (CONCAT(REGEXP(date, '[0-9]{4}'),
REGEXP(date, '/[0-9]{2}/'), REGEXP(date, '^[0-9]{4}/')) DESC
I think I need to use substring_index somehow, because regexp just returns 1 or 0, not the actual value found.
You need to convert your varchar-stored date objects into DATE objects, then use them to order.
This you can do on the fly like so
ORDER BY STR_TO_DATE(date,'%d/%m/%Y') DESC
But performance is going to be horrible. For best results store your dates in a DATE column in your table.
you can use STR_TO_DATE
SELECT *
FROM Table1
ORDER BY STR_TO_DATE(date, '%d/%m/%Y') desc,
sales desc
Code:
$SQLString = "SELECT DISTINCT NodeNumber, NodeLocation
FROM graphnode
ORDER BY NodeNumber ASC"
Output:
1000
1001
1002
101
1010
One of my friend is facing this problem and I don't know the complete project details. Any ideas what can be happening here.
the column NodeNumber seems to be in string format, convert it to numeric first, example
SELECT..
FROM..
WHERE..
ORDER BY CAST(NodeNumber AS SIGNED) ASC
SQLFiddle Demo
your column type is not set to Number Format like Int, it is set to Varchar or some other String Format, you can cast it further and you don't need to give ASC as default order is ASC only not DESC.