Today I did something stupid: I had a list of card numbers in an excel file that I had to import to a DB table somehow. So i exported the numbers to CSV file, but without any quotes (don't ask me why). The file looked like:

123456
234567
345678
...

Then I created a table with a single VARCHAR(22) column and did a

LOAD DATA LOCAL INFILE 'numbers.csv' INTO TABLE cards

This worked fine, apart from many warnings, which I ignored (the other stupid thing I did). After that I tried to query with this SQL:

SELECT * FROM cards WHERE number='123456'

which gave me an empty result. Whereas this works:

SELECT * FROM cards WHERE number=123456

Notice the missing quotes! So it seems, that I managed to populate my VARCHAR table with INTEGER data. I have no idea how that is possible at all.

I already tried to fix this with an UPDATE like this

UPDATE cards SET number = CAST(number AS CHAR(22))

But that didn't work.

So is there a way to fix this and how could this even happen?

有帮助吗?

解决方案

This is the result of some implicit conversion in order to do a numerical comparison:

SELECT * FROM cards WHERE number='123'

This will only match against text fields that are literally "123" and will miss on " 123" and "123\r" if you have those. For some reason, "123 " and "123" are considered "equivalent" presumably do to trailing space removal on both sides.

When doing your import, don't forget LINES TERMINATED BY '\r\n'. If you're ever confused about what's in a field, including hidden characters, try:

SELECT HEX(number) FROM cards

This will show the hex-dumped output of each string. Things like 20 indicate space, just as %20 in a URL is a space.

You can also fix this by:

UPDATE cards SET number=REPLACE(number, '\r', '')
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top