;

How to Get or Return the Server Name Using Query in SQL Server (T-SQL)


Tutorialsrack 06/03/2021 SQL SQL Server

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. 

  1. @@SERVERNAME
  2. SERVERPROPERTY() Function

@@SERVERNAME

@@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:

Example - @@SERVERNAME
SELECT @@SERVERNAME AS 'SERVER NAME';
Output

SERVER NAME

------------------------

LAPTOP-LH3JFI6M

SERVERPROPERTY() Function

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:

Example - SERVERPROPERTY() Function
SELECT SERVERPROPERTY('ServerName') AS 'SERVER NAME';
Output

SERVER NAME

---------------------------------

LAPTOP-LH3JFI6M

In my case, the output of both the function is the same.

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!


Related Posts



Comments

Recent Posts
Tags