Monday, March 19, 2012

How do I get SQL Server version (numbers only) programatically

Hi
I want to get SQL Server version. I know this way: Select @.@.version
But this gives me a very long string, i.e.
Microsoft SQL Server 2000 - 8.00.194 (Intel X86) Aug 18 2006
00:57:48
Copyright (c) 1988-2000 Microsoft Corporation Personal Edition on
Windows
NT 5.0 (Build 2195: Service Pack 4)
i want this value in a c++ application, and depending upon that i have
to fire query.
I just want the the major version number 6.0 ,7.0 , 8.0 or whatever
maybe.
How do I get these numbers only?
Regards,
Ravi ShankarFor the more recent versions of SQL Server, you can use SELECT
SERVERPROPERTY('ProductVersion'). Otherwise, you'll need to parse the
@.@.VERSION string. Transact-SQL example:
DECLARE @.Version nvarchar(125)
IF SERVERPROPERTY('ProductVersion') IS NOT NULL
BEGIN
SELECT SERVERPROPERTY('ProductVersion')
END
ELSE
BEGIN
SET @.Version =
SUBSTRING(@.@.VERSION, CHARINDEX('- ',
@.@.VERSION) + 2, 13)
SET @.Version = LEFT(@.Version, CHARINDEX(' ',
@.Version))
SELECT @.Version
END
Hope this helps.
Dan Guzman
SQL Server MVP
<ravidhari@.gmail.com> wrote in message
news:1156937494.514331.282750@.m73g2000cwd.googlegroups.com...
> Hi
> I want to get SQL Server version. I know this way: Select @.@.version
>
> But this gives me a very long string, i.e.
>
> Microsoft SQL Server 2000 - 8.00.194 (Intel X86) Aug 18 2006
> 00:57:48
> Copyright (c) 1988-2000 Microsoft Corporation Personal Edition on
> Windows
> NT 5.0 (Build 2195: Service Pack 4)
> i want this value in a c++ application, and depending upon that i have
> to fire query.
> I just want the the major version number 6.0 ,7.0 , 8.0 or whatever
> maybe.
> How do I get these numbers only?
>
> Regards,
> Ravi Shankar
>|||SELECT SERVERPROPERTY('ProductVersion')
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com|||Ravi,
You can try using "exec master..xp_msver".
It will return a recordset containing lots of info about your SQL Server
installation. One of the records will contain information about the
"ProductVersion".
Cheers!
SQLCatz

No comments:

Post a Comment