The problem, as mentioned by @bfavaretto in the comments, is that you are probably storing your numerical data as strings.
In addition, if you are looking for exact values, the operator is the =
and not the LIKE
.
A possible solution is:
SELECT * FROM Codigos WHERE CAST(Codigo AS UNSIGNED) = 123;
(use SIGNED
if it has negative numbers)
Or even:
SELECT * FROM Codigos WHERE 0 + Codigo = 123
but this should not be used definitively, if your data is always numerical or null.
ZEROFILL
Note that you can create numeric columns with prefix 0
using the option ZEROFILL
, is one of the Mysql column options. In this case, just don’t use quotes:
SELECT Codigo FROM Codigos WHERE Codigo = 123
Result, assuming the column is defined as INT(6) ZEROFILL
:
000123
More details on mysql manual.
The real problem is that you are writing numbers as text (varchar). In text, "1" and "01" are different.
– bfavaretto
SELECT * FROM Codigos WHERE 0 + Codigo = 123
- is to "fix" the problem @bfavaretto has already mentioned. The correct thing is to fix the DB. If you need zeros, control in the view instead of storing.– Bacco
Thank you, in case I’ll arrange in the comic then, since I only have to store whole numbers, and in case I standardize not to be used the Zero left! so it’s easier for me to treat later.
– JonatasM