How to find SQL Server Instance Name by using TSQL

Scenario:

Often as SQL Server DBA or SQL Server Developer we have to find the SQL Server instance name. Below query can be used to find out the SQL Server Instance name.

Solution:

You can use global variables @@servername and @@sqlservicename to find out the SQL Server Instance name.


SELECT @@servername  AS servername,  @@ServiceName AS sqlservicename 
--If SQL Server is installed in Default 
ServerName                                                       SQLServiceName
YourComputerName                                           MSSQLSERVER
--If SQL Server is intalled as Named Instance
ServerName                                                        SQLServiceName
YourComputerName\InstanceName                   InstanceName
--If SQL Server is intalled in ClusterMode
ServerName                                                        SQLServiceName
NetworkName\InstanceName                             InstanceName