Check SQL Server Version in T-SQL
[SQL Server, TSQL]
If you are working with Microsoft SQL Server as your database, you might find yourself in a position where you need to verify the version of SQL Server that you are connected to.
Perhaps you just want to confirm the capabilities, or you have some conditional logic that is version-specific.
You can interrogate the server and query the SERVERPROPERTY property.
The property you are interested in here is ProductVersion
You fetch it like this:
SELECT
SERVERPROPERTY('ProductVersion') AS ProductVersion;
This will return a result like this:
16.0.4215.2
The leading 16 will tell you the version of SQL Server.
The other values are as follows:
0- minor version4215- build version2- revision
Below are the major versions of SQL Server that are still supported.
| Major | SQL Server Version | Year |
|---|---|---|
| 16.x | SQL Server 2022 | 2022 |
| 15.x | SQL Server 2019 | 2019 |
| 14.x | SQL Server 2017 | 2017 |
| 13.x | SQL Server 2016 | 2016 |
The next version of SQL Server, SQL Server 2025, will presumably be major version 17.
TLDR
You can query the SERVERPROPERTY to extract the version of SQL Server you are connected to.
Happy hacking!