How To Find Database Name In SQL Server

Finding database name in SQL servers is a daily task for an SQL developer. In this article, I will guide you through multiple ways to find a database name in SQL server with some real-time examples.

How To Find Database Name In SQL Server

Let us discuss all those approaches individually.

Approach-1: Using sys.databases

To find database name in SQL server, we can execute the query below.

SELECT name AS DatabaseName
FROM sys.databases
ORDER BY name;

After executing the above query, I got the expected output as shown in the screenshot below.

How To Find Database Name In SQL Server

If you wish to exclude the lists of system databases, you can execute the above query with the where condition as specified below.

SELECT name AS DatabaseName
FROM sys.databases
WHERE database_id > 4
ORDER BY name;

After executing the above query, I got the expected output below.

how to find the name of the database in sql server

Check out How to insert a row in a view in SQL Server

Approach-2: Using SQL Server Management Studio (SSMS) Object Explorer

Follow the below simple steps

1. Connect to your SQL server instance in SSMS.

2. Once connected, expand the database folder under Object Explorer.

how to find database name in sql server management studio

Check out View Line Numbers in SQL Server Management Studio

Approach-3: sp_databases stored procedure

We can also execute the sp_databases stored procedure for this purpose. This will provide the names of all the databases, including their size. We need to execute the below query.

EXEC sp_databases;

I got the expected output successfully after executing the above query.

query to find database name in sql server

Approach-4: Using the DB_NAME() Function

We can also use the DB_NAME() function to find out the name of my current SQL server database.

SELECT DB_NAME() AS CurrentDatabase;

After executing the above function, I got the expected output, as shown below.

how to find a database name in sql server

Video Tutorial

Conclusion

Finding the name of the databases in SQL server is never difficult using the different approaches mentioned in this article. You can use sys.databases, sp_databases, DB_NAME(), and SQL server management Studio (SSMS) design approach for this purpose.

You may also like following the articles below.

Top 200 SQL Server Interview Questions and Answers

Free PDF On Top 200 SQL Server Interview Questions And Answers

Download A 40 pages PDF And Learn Now.