If I have a field that is all numerical, but is set up as varchar, will it cause any problems in the already-entered data if I alter it to make that field an int field?
It probably will be unless you've formatted the numeric strings with leading zeros, which make alphabetic and numeric sorting the same.
I always ask in this type of situation because I've seen too many database schema which used numeric types when string types were more appropriate. One classic case was an type-ID field in an billing-by-hours application -- the field was formatted like "7.1", and by the programmer's using a float to store the field, the software ran into all kinds of data precision errors.
My general rule of thumb has been that unless you're planning on performing numeric calculations on the data, it should be usually be stored in a string type.
In that case, WOULD it be possible to search a range on the string, thus not needing to change the field type?
The field is a price, and I need to be able to search - for example - everything less than 100,000 or anything above 100,000 or everything between 100,000 and 150,000... ?
If you are searching for values that arithmetically fall between two given values, then you are numerically ordering the data. "Between", "greather than" and "less than" only have meaning in the context of an implicit ordering.
This falls into "performing numerical operations" in my general rule.
Again, the only way you can leave the value as a string and still do meaningful searching is that you have formatted all these numerical strings to a fixed length with left-padding of zeroes.
Otherwise, when you perform the search, "8000" will be found to be between "75000" and "90000".
So you must either left-zero-pad all these values to a fixed length or convert them to numbers. I recommend the latter.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.