Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to compare two varchar values with leading zeros in mysql

Tags:

sql

mysql

varchar

I have to compare 2 columns in mysql which has varchar data type. But there are some data which has leading '0' which is creating problem while comparing.

For ex: I have to compare between '02653' and '2653' which are actually equal, but here my query fails and gives different result.

Please suggest any idea what should be the right query for this.

I have tried to run like this SELECT DISTINCT table1.store_id FROM table2 WHERE CONVERT('first_column' AS INTEGER) = CONVERT('second_column' AS INTEGER)

Showing this error
** for the right syntax to use near AS INTEGER **

like image 221
user1860373 Avatar asked Dec 11 '25 01:12

user1860373


2 Answers

convert the varchar to integer and then compare

like image 90
Debobroto Das Avatar answered Dec 12 '25 15:12

Debobroto Das


...
WHERE CONVERT(column1 AS INTEGER) = CONVERT(column2 AS INTEGER)
like image 23
xlecoustillier Avatar answered Dec 12 '25 17:12

xlecoustillier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!