In this article, you’ll learn how to get or return the server name using a query in SQL Server.
There are 2 ways we are using in this post to return the server name using a query in SQL Server.
@@SERVERNAME
SERVERPROPERTY()
Function@@SERVERNAME
used to return the name of the local server that is running SQL Server. To get the server name, you can simply use this query as given below:
SELECT @@SERVERNAME AS 'SERVER NAME';
SERVER NAME
------------------------
LAPTOP-LH3JFI6M
SERVERPROPERTY()
Function is a metadata function that is used to return property information about the server instance.
To get the server name, you can simply use this query as given below:
SELECT SERVERPROPERTY('ServerName') AS 'SERVER NAME';
SERVER NAME
---------------------------------
LAPTOP-LH3JFI6M
In my case, the output of both the function is the same.
Note:- The ServerName property of the SERVERPROPERTY
function and @@SERVERNAME
return similar information. The ServerName
property provides the Windows server and instance name that together make up the unique server instance. @@SERVERNAME
provides the currently configured local server name. The ServerName
property and @@SERVERNAME
return the same information if the default server name at the time of installation has not been changed.
The SERVERNAME
property automatically reports changes in the network name of the computer.
@@SERVERNAME
does not report such changes. @@SERVERNAME
reports changes made to the local server name using the sp_addserver
or sp_dropserver
stored procedure.I hope this article will help you to understand how to get or return the server name using a query in SQL Server(T-SQL).
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments