Some times it is required to get database ID to be used in a script. Or it is required to get database name from database ID. One approach is to make a join with sys.database. But i want to point out a very handy function for these tasks.
To get database ID from database name use following command
SELECT DB_ID(‘AdventureWorks’) GO
And similarly to get database name from database ID use following command
SELECT DB_NAME(5) GO
So these make the tasks easy.