Max value from varchar - mysql

i have a column in a database in varchar format, but the content is only numbers.
I cant change the column to int format, and need to use the varchar format.
How it possible to find the highest number?
Category
-------
1
2
3
4
5
6
7
8
9
10
11
12
if I use this
SELECT * FROM [Navision4].[dbo].[" & strCompany & "$eShopCategory]
ORDER BY Category desc
it my output 10, but i want the output to be 12.

What if you CAST category column to INT while performing sorting like
SELECT *
FROM [Navision4].[dbo].[" & strCompany & "$eShopCategory]
ORDER BY CAST(Category AS INT) desc
You can as well use MAX() function on Category column
SELECT MAX(CAST(Category AS INT))
FROM [Navision4].[dbo].[" & strCompany & "$eShopCategory]

select category from mytable
order by length(category) desc, category desc
limit 1
(or len(catgegory) if DB is actually SQL Server)

Related

Getting MAX int column but avoiding natural order MySQL

I need to get the biggest folio number record from a services table:
services
- id
- folio (int)
The folio number is an Int column formed by year + incremental number. Every time a record is inserted the folio number is formed by the_current_year + (max folio found + 1) Saying that, this is a sample list of available folios:
20191
...
2019124
2019125
20201
20202
...
202019
As per the sample list, I would have 125 services for year 2019 and 19 services for 2020 so far. Please note that on every year change, the latest digits for the folio number start again from 1.
I'm facing 2 issues here. Treating it as integer and getting the MAX by folio won't work because natural sort order. It will return the biggest int.
So doing:
SELECT MAX(folio) FROM services LIMIT 1; returns 2019125 when I actually need to get 202019
Treating it as varchar won't either work, since it will be ordered by char:
SELECT MAX(CONVERT(folio, CHAR(50))) FROM services LIMIT 1; returns 20202 instead of 202019
So my question is how to get the latest folio number?
This will do it:
SELECT folio
FROM services
ORDER BY LEFT(folio, 4) DESC, SUBSTR(folio, 5) + 0 DESC
LIMIT 1
In MySql you can treat integers as strings and apply functions like LEFT() and SUBSTR().
By applying +0 to a string, the string is implicitly converted to a number.
See the demo.
Results:
| folio |
| ------ |
| 202019 |
You would be better off having the year and the serial number in separate columns. You can separate them in the query:
SELECT folio
FROM (
SELECT
CAST(substring(folio, 1,4) AS UNSIGNED) as 'year',
CAST(substring(folio, 5) AS UNSIGNED) as 'service_no',
folio
FROM services
ORDER BY year DESC, service_no DESC
limit 1
) AS q;
See db-fiddle

MySQL and PHP using rank by ASC

Trying to fix ranking off scores table, I put this in;
SELECT * from scores where tournament_id = "3" AND class = "MA1" ORDER BY Rank ASC
It came out
1
11
12
13
2
22
3
31
4
5
6 and on, how can it be fixed?
Thanks
Your rank column data type is string. So it is returning as above please convert it in number or int format.
SELECT CAST("10" as SIGNED) ;
Seems like your rank column is a textual column (e.g., varchar), and therefor, when you order by it, you order lexicographically. If you want to sort it numerically, you need to explicitly convert it:
SELECT *
FROM scores
WHERE tournament_id = "3" AND class = "MA1"
ORDER BY CAST(rank AS INTEGER) ASC
-- Here -^
SELECT * from scores
where tournament_id = "3"
AND class = "MA1"
ORDER BY CAST(Rank AS INTEGER) ASC
You should casting the type char to int your rank field for order by

Mysql Order by number dont humanize output

my mysql query dont humanize the ORDER BY.
SELECT COUNT(level) as count, level
from logtest
GROUP BY level
ORDER BY level;
Sample:
6
5
5
5
13
0
The correct would be
13
6
5
5
5
0
any help?
SELECT COUNT(level) as count, level
FROM logtest
GROUP BY level
ORDER BY cast(level as unsigned);
Near Dup of: Cast from VARCHAR to INT - MySQL
Not quite a dup because the issues on sort is that it's sorting by a text field (my guess is level is of character type) when you want it to sort by numeric type... so just cast which the above link describes how.

Mysql search text in one column

For Example our table;
İd--------Price---------Level
1 ------100-300 ------ 1,2
2 ---------200----------1
3 ------100-280--------1,3
We want search a price value is 110. 110 is between 100-300 and 100-280 so id 1 and id 2 must listed. Can we write this query with my-sql?.
Additional , we want search price and level value. Price 110 and level 2 searching. Can we write this query with my-sql?.
Thank You
Remember that database tables should be created with the idea that it will satisfy your query needs. It doesn't make sense to have a table with a price "100-300" which represents a String (or in mysql a VARCHAR) and you want to treat this as a number. So what to do?
1) The first thing i would do is re write my table schema having a minPrice and maxPrice fields, so this way you could have this:
İd----minPrice---maxPrice------level
1 ------100---------300 ------ 1,2
2 ------200---------200----------1
3 ------100---------280--------1,3
2) Then your query would be like:
SELECT id FROM Mytable x WHERE myValue >= x.minPrice AND myValue <= x.maxPrice
3) In case you also want to look for a level value. you would do:
SELECT id FROM Mytable x WHERE myValue >= x.minPrice AND myValue <= x.maxPrice AND myLevelValue IN (x.level)
Use two columns for the price ...
ID FROM TO LEVEL
1 100 300 1,2
2 200 200 1
3 100 280 1,3
Then SQL:
SELECT `level` FROM `table`
WHERE X >= `from` AND X <= `to`

MySql Select Id <= Maximum value in the table

When using the <= in a MySql statement, is it possible to make MySql select the maximum value in a table without supplying a value to <= in the sql statement?
Eg:
id
----
1
2
3
4
5
6
Eg:
// start from the last record when no value is supplied
select id from table where id <= * ORDER BY id DESC LIMIT 5
Result
6
5
4
3
2
// start from the 5th record when a value is supplied
select id from table where id <= 5 ORDER BY id DESC LIMIT 5
Result
5
4
3
2
1
My problem is, this statement is in a php function, so I cannot change it dynamically. Is it somehow possible to do what I'm trying, or perhaps another way to get around this?
You can use NULL instead of using *
SET #var_search_value = NULL -- OR 5
SELECT id
FROM table
WHERE id <= #var_search_value OR #var_search_value IS NULL
ORDER BY id DESC LIMIT 5
If you want to get every record which is less than or equal to the maximum value in a particular column, then logically you want to get every record:
select id from table ORDER BY id DESC LIMIT 5
No WHERE clause is required.