Gwendal
Gwendal

Reputation: 1273

Varchar or decimal and cast

I've a table filled with products. Some of their characteristics like width and height are decimal. Until now, we were using floats but there was some problems with select not working, like:

 SELECT ... WHERE width = 3.4 

was giving no results, while

SELECT ... WHERE width LIKE "3.4%" 

was.

So I'm looking to change column's type and I've the choice between varchar and decimal. I don't want any trailing zeroes on my frontend so if I go for decimal type, I'll have to change all my models to cast characteristics as char and then removing zeroes which could induce a lot of bugs.

What's the best type to choose knowing that I don't have any calculations to do on those characteristics and I only have to select with equalities, no greater than / less than.

Thanks for any help.

EDIT:

I tried to change columns' type to decimal, and casting to char to removing trailing zeroes take too much time, from 25ms to 500ms. I guess I've no other choice but removing them with php :(

Upvotes: 0

Views: 102

Answers (1)

Avindu Hewa
Avindu Hewa

Reputation: 1788

Using varchar will probably give you more work compared to using decimals , and also varchar might make your variables lose precision when casting . so i suggest sticking with decimals

Upvotes: 1

Related Questions