I have a table with a colum in it like this:TripID NumDaysXXX '9 days/8 nights'XXX '10-11 Days'XXX '1,2, or 3 days'XXX '8'XXX '129 Luxurious days'
I want to search on the Numdays column similar to this:SELECT TripID FROM tblTripsWHERE NumDays BETWEEN 10 and 15Obviously, that raises an error on all but the records with just a number in NumDays. I wrote a function to do a conversion...CREATE FUNCTION fnSafeConvertVarchar2Int (@STR VARCHAR(50))RETURNS INT ASBEGIN DECLARE @i int set @i = 1 WHILE @i <= LEN(@STR) BEGIN IF(NOT ISNUMERIC(SUBSTRING(@STR, 1, @i)) = 1) break set @i = @i + 1 END RETURN SUBSTRING(@STR, 1, @i - 1)END
... but the function fails on '1,2 or 3 days'. Apparently '1,2' is counted as numeric (it would be equal to 1.2 in Europe I guess).Alternately, is there some way to catch the errors generated by the conversion? So I could do WHILE (@@ERROR = 0 AND @i < LEN(@foo)) BEGIN SELECT @result = CAST(SUBSTRING(@foo,1,@i) AS int) set @i = @i + 1END
so as soon as it his some substring it couldn't handle, it would return the result of the last successful conversion.Thanks for the help,steve