130

Is there a system stored procedure to get the version #?

Ray
  • 187,153
  • 97
  • 222
  • 204

16 Answers16

238

Try

SELECT @@VERSION 

or for SQL Server 2000 and above the following is easier to parse :)

SELECT SERVERPROPERTY('productversion')
     , SERVERPROPERTY('productlevel')
     , SERVERPROPERTY('edition')

From: http://support.microsoft.com/kb/321185

Jake
  • 604
  • 3
  • 9
  • 33
Joe Kuemerle
  • 6,338
  • 1
  • 23
  • 18
  • 1
    The second one works for me, and i add to check on Wikipedia to understand that 8.00.xx means SQL server 2000 – pdem Apr 28 '15 at 07:52
31

SELECT @@VERSION

Brannon
  • 25,687
  • 5
  • 39
  • 44
29

I know this is an older post but I updated the code found in the link (which is dead as of 2013-12-03) mentioned in the answer posted by Matt Rogish:

DECLARE @ver nvarchar(128)
SET @ver = CAST(serverproperty('ProductVersion') AS nvarchar)
SET @ver = SUBSTRING(@ver, 1, CHARINDEX('.', @ver) - 1)

IF ( @ver = '7' )
   SELECT 'SQL Server 7'
ELSE IF ( @ver = '8' )
   SELECT 'SQL Server 2000'
ELSE IF ( @ver = '9' )
   SELECT 'SQL Server 2005'
ELSE IF ( @ver = '10' )
   SELECT 'SQL Server 2008/2008 R2'
ELSE IF ( @ver = '11' )
   SELECT 'SQL Server 2012'
ELSE IF ( @ver = '12' )
   SELECT 'SQL Server 2014'
ELSE IF ( @ver = '13' )
   SELECT 'SQL Server 2016'
ELSE IF ( @ver = '14' )
   SELECT 'SQL Server 2017'
ELSE
   SELECT 'Unsupported SQL Server Version'
Rich Benner
  • 7,873
  • 9
  • 33
  • 39
Mark Kram
  • 5,672
  • 7
  • 51
  • 70
14

For SQL Server 2000 and above, I prefer the following parsing of Joe's answer:

declare @sqlVers numeric(4,2)
select @sqlVers = left(cast(serverproperty('productversion') as varchar), 4)

Gives results as follows:

Result   Server Version
8.00     SQL 2000
9.00     SQL 2005
10.00    SQL 2008
10.50    SQL 2008R2
11.00    SQL 2012
12.00    SQL 2014

Basic list of version numbers here, or exhaustive list from Microsoft here.

Geoff
  • 8,551
  • 1
  • 43
  • 50
  • I like this, nice, simple and resusable across server versions. I used a slightly modified version of the above: `select cast(serverproperty('productversion') as varchar) as [result]`. My point is that I can execute the above via ADO.NET's `ExecuteScalar` and then parse the result string as a `System.Version` object. Also, conveting it into numeric gives different meanings to version numbers when it comes to trailing zeros and version segment digit count, while a string can be parsed to a valid `Version` object without losing the consistency of each version component. – Ivaylo Slavov Apr 24 '15 at 08:05
4

There is another extended Stored Procedure which can be used to see the Version info:

exec [master].sys.[xp_msver]
Palec
  • 12,743
  • 8
  • 69
  • 138
Zia
  • 41
  • 1
4
CREATE FUNCTION dbo.UFN_GET_SQL_SEVER_VERSION 
(
)
RETURNS sysname
AS
BEGIN
    DECLARE @ServerVersion sysname, @ProductVersion sysname, @ProductLevel sysname, @Edition sysname;

    SELECT @ProductVersion = CONVERT(sysname, SERVERPROPERTY('ProductVersion')), 
           @ProductLevel = CONVERT(sysname, SERVERPROPERTY('ProductLevel')),
           @Edition = CONVERT(sysname, SERVERPROPERTY ('Edition'));
    --see: http://support2.microsoft.com/kb/321185
    SELECT @ServerVersion = 
        CASE 
            WHEN @ProductVersion LIKE '8.00.%' THEN 'Microsoft SQL Server 2000'
            WHEN @ProductVersion LIKE '9.00.%' THEN 'Microsoft SQL Server 2005'
            WHEN @ProductVersion LIKE '10.00.%' THEN 'Microsoft SQL Server 2008'
            WHEN @ProductVersion LIKE '10.50.%' THEN 'Microsoft SQL Server 2008 R2'
            WHEN @ProductVersion LIKE '11.0%' THEN 'Microsoft SQL Server 2012'
            WHEN @ProductVersion LIKE '12.0%' THEN 'Microsoft SQL Server 2014'
        END

    RETURN @ServerVersion + N' ('+@ProductLevel + N'), ' + @Edition + ' - ' + @ProductVersion;

END
GO
Alex
  • 423
  • 4
  • 7
3

Here's a bit of script I use for testing if a server is 2005 or later

declare @isSqlServer2005 bit
select @isSqlServer2005 = case when CONVERT(int, SUBSTRING(CONVERT(varchar(15), SERVERPROPERTY('productversion')), 0, CHARINDEX('.', CONVERT(varchar(15), SERVERPROPERTY('productversion'))))) < 9 then 0 else 1 end
select @isSqlServer2005

Note : updated from original answer (see comment)

Bruce Chapman
  • 1,227
  • 3
  • 12
  • 19
  • Just found this doesn't work sql 2008 because '10' is less than '9'. You can change the value in the updated answer to use 8, 9, 10 or whatever you need to test as a minimum value – Bruce Chapman Jun 29 '11 at 07:01
2

The KB article linked in Joe's post is great for determining which service packs have been installed for any version. Along those same lines, this KB article maps version numbers to specific hotfixes and cumulative updates, but it only applies to SQL05 SP2 and up.

Community
  • 1
  • 1
Matt
  • 5,052
  • 4
  • 36
  • 54
1
SELECT 
@@SERVERNAME AS ServerName,
CASE WHEN LEFT(CAST(serverproperty('productversion') as char), 1) = 9 THEN '2005'
 WHEN LEFT(CAST(serverproperty('productversion') as char), 2) = 10 THEN '2008'
 WHEN LEFT(CAST(serverproperty('productversion') as char), 2) = 11 THEN '2012'
END AS MajorVersion,
SERVERPROPERTY ('productlevel') AS MinorVersion, 
SERVERPROPERTY('productversion') AS FullVersion, 
SERVERPROPERTY ('edition') AS Edition
crosswalk
  • 21
  • 1
1

Getting only the major SQL Server version in a single select:

SELECT  SUBSTRING(ver, 1, CHARINDEX('.', ver) - 1)
FROM (SELECT CAST(serverproperty('ProductVersion') AS nvarchar) ver) as t

Returns 8 for SQL 2000, 9 for SQL 2005 and so on (tested up to 2012).

Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
Nux
  • 9,276
  • 5
  • 59
  • 72
1

Try this:

if (SELECT LEFT(CAST(SERVERPROPERTY('productversion') as varchar), 2)) = '10'
BEGIN
animuson
  • 53,861
  • 28
  • 137
  • 147
freak
  • 11
  • 1
1

If all you want is the major version for T-SQL reasons, the following gives you the year of the SQL Server version for 2000 or later.

SELECT left(ltrim(replace(@@Version,'Microsoft SQL Server','')),4)

This code gracefully handles the extra spaces and tabs for various versions of SQL Server.

user489998
  • 4,473
  • 2
  • 29
  • 35
1

Try

SELECT @@MICROSOFTVERSION / 0x01000000 AS MajorVersionNumber

For more information see: Querying for version/edition info

VAV
  • 1,756
  • 1
  • 16
  • 26
1
select substring(@@version,0,charindex(convert(varchar,SERVERPROPERTY('productversion')) ,@@version)+len(convert(varchar,SERVERPROPERTY('productversion')))) 
0

Try this:

SELECT @@VERSION[server], SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition')
malioboro
  • 3,097
  • 4
  • 35
  • 55
Arif
  • 1
  • 1
-1

Try this:

SELECT
    'the sqlserver is ' + substring(@@VERSION, 21, 5) AS [sql version]
MAXE
  • 4,978
  • 2
  • 45
  • 61