Get Database Name from Database ID and Database ID from Database Name in SQL Server

To get Database Name from Database ID run the following query (in this example Database ID is 2):

SELECT DB_NAME(2) AS Database_Name;

Get Database Name from Database ID in SQL Server

To get Database ID from Database Name run the following query (in this example Database Name is tempdb):

SELECT DB_ID('tempdb') AS Database_ID;

Get Database ID from Database Name in SQL Server

To get the list of all Database Names and Database IDs on the SQL Server instance:

SELECT name as Database_Name, database_id as Database_ID
FROM sys.databases;

List of all Database Names and Database IDs on the SQL Server instance

If you want to see how to get the name or Database ID of the current Database in SQL Server, read related article:
Get the Name or ID of the current Database in SQL Server

If the post helped you, please share it:
Pin It

3 comments to Get Database Name from Database ID and Database ID from Database Name in SQL Server

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">