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

How to ignore a varying number of starting 0s in a SQL column for group by

I am using the following query to find repeated numbers in my table:

SELECT 
    c.code,
    COUNT(c.code) 
FROM cards c
group by c.code
HAVING COUNT(c.code)>1;

But the column code has varying numbers of 0s before the numbers, as an example:

0001897
001897
01897
1897

How could I make that query ignore all 0s before the numbers to consider all of the above as repeated (the same row in a group by)?

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 try to use TRIM function with LEADING '0'

SELECT 
    TRIM(LEADING '0' FROM c.code),
    COUNT(c.code) 
FROM cards c
group by TRIM(LEADING '0' FROM c.code)
HAVING COUNT(c.code)>1;

SQLFIDDLE

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