I directly access the internal MS SQL Server software package. The key field (vehicle number) is defined as alpha, but we enter a numerical value in the field. There is only one exception, we put an “R” in front of the number when the car retires (this means that we sold it or the car was hacked). Assuming that users are doing this correctly, we should not run into a problem using this method. (Right or wrong is not a problem here)
Fast Forward I am trying to request a subset of these car numbers (800 - 899) for some special handling. Making the range from “800” to “899” we also get 80, 81, etc. If I specify the car number in INT, I should be able to get the correct range. Except that these "R" cars are kicking me in the ass now.
I tried where vehicleId not like 'R%' and cast(vehicleId as int) between 800 and 899, however, I got a casting error on one of these R cars.
Which works where vehicleId not between '800' and '899' and cast(vehicleId as int) between 800 and 899', but I believe there should be a better way and a less confusing way.
I also tried other options with HAVINGand a subquery all producing a casting error.
source
share