get dbid or database name in sql server

I have seen many times DBA / developers are joining the sysdatabase table in master database to get the database name or database id. There are two builtin sql server functions for this purpose, lets see how to get the database name from dbid or how to get the dbid from database name.

SELECT DB_ID('Database_Name') as [Database ID]

The above T-SQL query is using DB_ID( ) function to get the database id from database name.

SELECT DB_Name(1) as [Database Name]

The second query will give you the database name, the DB_Name ( ) function takes database id as parameter and returns database name.

In both functions if no value is found i.e database name or database id does not exists then a NULL value is returned.




Add Comments

Name: *
Email: *
URL:
Comments: *