Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare software versions using SQL Server?

Tags:

When trying to compare software versions 5.12 to 5.8, version 5.12 is newer, however mathematically 5.12 is less than 5.8. How would I compare the two versions so that a newer version returns 'Y'?

SELECT CASE WHEN 5.12 > 5.8 THEN 'Y' ELSE 'N' END

Possible Solutions

  1. Add a 0 after the decimal in 5.8 so that it compares 5.08 to 5.12, however it seems like this would require a bit of code.
  2. Simply compare values after the decimal (ie. 12 > 8), however this fails when the version rolls to 6.0.
  3. Use reverse logic and assume that if 5.12 is less than 5.8 to return 'Y'. I believe this would fail when the version rolls to 6.0.
like image 891
Kermit Avatar asked Jul 06 '12 14:07

Kermit


People also ask

What is SQL Compare tool?

SQL Compare allows you to work with live SQL Server databases, snapshots, script folders, and native backups. You can compare your SQL Server databases, analyze differences, and perform error-free schema synchronization using generated SQL scripts.

Can we compare two databases in SQL Server?

You can also compare the schema of two databases or of two versions of the same database. For more information, see How to: Use Schema Compare to Compare Different Database Definitions.


1 Answers

You could use hierarchyid Which you can use by putting a / at the end and start of the string and casting it

e.g.

SELECT CASE WHEN cast('/5.12/' as hierarchyid) > cast('/5.8/' as hierarchyid) THEN 'Y' ELSE 'N' END

That returns a Y

like image 66
Eva Lacy Avatar answered Sep 22 '22 14:09

Eva Lacy