Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find smallest number from strings

I have really big number stored in my db.
They looks like this:

14379999999999997549324988728489489203
25899999999999995535156495356264448
47247499999999996139311253533698818048
6338599999999999164916227526098944
6341299999999999522809053905420288

I store them like this: big_number VARCHAR(100)

How to get lowest number of these values? Number length could be same.
Or should i somehow rebuild my db?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can compare numbers as strings, that is alphabetically, when they have the same length.

So you’re looking for the shortest number and the first one when sorted alphabetically.

Something like:

SELECT big_number 
FROM table_name 
ORDER BY LENGTH(big_number), big_number 
LIMIT 1. 

Here’s a demo: https://www.db-fiddle.com/f/2VowxbkxnhQahaY9WUxDZQ/0

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading