Get Database Names from SQL Server

In this article, we will show how to write a query to Get Database Names from SQL Server with an example. The list of databases in SQL Server that are available in our current instance.

View DBS in Object Explorer 1

Get Database Names in SQL Server Example

Here, we will show you how to Get database names in Sql Server

USE master
GO
SELECT name FROM sys.databases
Get Database Names from SQL Server 2

You can also use sysdatabases to get the list of databases in SQL Server.

USE master
GO
SELECT name FROM sysdatabases
SELECT name FROM sysdatabases 3

Or, use sp_databases stored procedure to get a list of databases in Sql Server.

USE master
GO
EXEC sp_databases
EXEC sp_databases 4

Get Database Names Example 2

In this example, we will restrict the result. I mean, we will get Database names in a Server without system databases. If you know the database id, use the following query to display the list of databases except for system databases.

USE master
GO
SELECT database_id, name FROM sys.databases
WHERE database_id > 4
SELECT database_id, name FROM sys.databases WHERE database_id > 4

Or, try NOT IN operator.

USE master
GO
SELECT database_id, name FROM sys.databases
WHERE name NOT IN ('master', 'model', 'msdb', 'tempdb')
Get Database Names Except System DBS 6
Categories SQL