Friday, 27 March 2015

Restore database BAK file to database

Steps to Restore BAK FIle to Database

1)Retrieve the logical file name of the database
RESTORE FILELISTONLY
FROM DISK = 'E:\YourBaackUpFile.bak'
GO

Step 2: set database to single user
ALTER DATABASE DatabaseName
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE
GO

Step 3: Restore database with the help of the logical file name retrieved in step 1.
RESTORE DATABASE YourDB
FROM DISK = 'D:\YourBaackUpFile.bak'
WITH MOVE 'YourMDFLogicalName' TO 'E:\DataYourMDFFile.mdf',
MOVE 'YourLDFLogicalName' TO 'E:\DataYourLDFFile.ldf'
GO

Step 4: Bring database to multi user mode.
ALTER DATABASE YourDB SET MULTI_USER
GO

Here are all the four steps together:

Days in a Year (SQL Server)

1) The first solution

--Work with SQL server 2005 and above.

DECLARE @year AS INT SET @year=2015

SELECT DATEPART(dy,(DATEADD(YEAR,@year-1899,0)-1)) as TotalDays

GO

2) Second Solution

--Work with SQL server 2012 and above.

DECLARE @year AS INT

SET @year=2015

SELECT DATEPART(dy,DATEFROMPARTS(@Year,12,31))

AS TotalDays

Go