T-SQL Tutorial

How to get SQL Server local server name

In SQL Server, you can obtain the local server name using Transact-SQL (T-SQL) in several ways. The most common method is by using the @@SERVERNAME system function. This function returns the fully qualified name of the local SQL Server instance. Here's how you can retrieve the local server name using T-SQL:

@@SERVERNAME function


When you run this query, it will return the local server name as a result.

@@SERVERNAME function

Alternatively, you can use the SERVERPROPERTY function to retrieve the local server name:

SELECT SERVERPROPERTY('MachineName') AS 'LocalServerName';

This query specifically fetches the machine name where the SQL Server instance is running, which is often the same as the local server name.

Keep in mind that these methods provide you with the local server name as it's configured within SQL Server. If you need the host or computer name where SQL Server is installed, it should usually match the local server name, but it may differ in certain network configurations.