kernelMode kernelMode - 7 months ago 8
SQL Question

SQL query to get sql year version

I'm using SQL Server and I need a SQL query which returns the year of the version.

I'm familiar with the next methods:

select @@version

select SERVERPROPERTY('productversion')


but I need a query that will return for example:

2008


or

2005


The first query I mentioned requires dirty parsing and the second returns some product number.

Do anyone knows such a query?

Thanks!

Answer

Try this - it just inspects the first two characters of the version number and returns the corresponding "year" for the SQL Server version.

SELECT
    'SQL Server Year ' + 
    CASE SUBSTRING(CONVERT(VARCHAR(50), SERVERPROPERTY('productversion')), 1, 2)
          WHEN '8.' THEN '2000'
          WHEN '9.' THEN '2005'
          WHEN '10' THEN '2008'
          WHEN '11' THEN '2012'
    END

Of course, you can package this up into a user-defined function to make it "prettier"