I have a MS SQL query that I am creating and I need to determine if a particular record falls into a “new” or “old” category based off of the version number.
There is a column ‘VersionNum’ and the old version is any version number from 2.75.99.99 and below while the new version is any version number from 2.76.00.00 and up. The data type for the column is varchar(20).
I was thinking about taking a subset of the string from X.XX and casting this as a decimal. Then once all versions are cast to a X.XX decimal I can determine old or new by performing a > or < in a case statement within the select.
Would this be the correct or idea way of going about deciding where the version numbers fall? Or is there some other method. I only ask because I would imagine that something similar to this is needed else where and this is not a unique situation.
EDIT: One additional issue. There are some versions that are not X.XX.XX.XX (2.76.00.00) and some are in the form: 8.00.00 and would be considered “Old” versions. This confuses the system a lot more than I once thought. I did get information that the 5 digit version (8.00.00) is considered old all the time. So whenever there are 5 digits, the version is old. So I am thinking I would perform a case statement similar to:
SELECT
Old = CASE WHEN LEN(VersionNum) < 10 THEN 1
WHEN CAST(SUBSTRING(VersionNum,1,4) AS DECIMAL(5,2)) < 1.52
AND LEN(VersionNum) >= 10 THEN 1
ELSE 0
END,
New = CASE WHEN CAST(SUBSTRING(VersionNum,1,4) AS DECIMAL(5,3)) >= 1.52
AND LEN(VersionNum) >= 10 THEN 1
ELSE 0
END
FROM
VersionTable
Is my logic correct? Or am I missing something. The inequality in the lengths is very annoying.
You can always express such a “number” as an integer, assuming it will always have only 4 parts:
Result:
So if you create a view or a computed column, you can compare using simple math instead of trying to do complicated parsing / conversion.
EDIT based on comment
If the table has some values that only have three octets, why not fix that?
If you want to be more precise: