Search code examples
sql-serversqlcmd

Get the SQL Server version to a variable in bash script


I want to get the SQL Server version into a variable in my bash script.

I tried using

sqlcmd -S localhost -U SA -Q 'select @@VERSION'

but it returns a string containing a bunch of other information too - eg: the copyright information, 86/x64? etc.

What is the correct way to get just the version?


Solution

  • Depends what you mean by 'SQL Server Version', but if you mean the build number:

    sqlcmd -S localhost -U SA -Q "SELECT CAST(SERVERPROPERTY('ProductVersion') AS NVARCHAR(128))"
    

    Example output:

    13.0.5081.1