{"id":256,"date":"2013-01-09T15:57:23","date_gmt":"2013-01-09T15:57:23","guid":{"rendered":"http:\/\/www.sqlfreelancer.com\/blog\/?p=256"},"modified":"2014-03-06T16:05:23","modified_gmt":"2014-03-06T16:05:23","slug":"restore-sql-server-databases-using-datetime-functions","status":"publish","type":"post","link":"https:\/\/www.sqlfreelancer.com\/blog\/restore-sql-server-databases-using-datetime-functions\/","title":{"rendered":"Restore SQL Server Databases using DateTime functions"},"content":{"rendered":"<p>If you take full backups using SQL Server maintenance plans and let SQL Server use the default naming convention, you have probably noticed that usually you&#8217;ll have file name in the format of &#8220;database name + backup + date + time + .bak&#8221;. For example, a backup from the master database may look like this: &#8220;master_backup_2012_10_02_220558_8601773.bak&#8221;.\u00a0 It can be a challenge to script out automatic restores because the numbers on the end of the backup name constantly change. In this tip I will explain how to script out RESTORE DATABASE statements using DateTime functions.<\/p>\n<p>Let&#8217;s say we have a folder full of backups like this:<\/p>\n<p><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2014\/03\/SQL-Server-Restore-DateTime.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-257\" alt=\"SQL Freelancer SQL Server Backup Restore Datetime Functions\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2014\/03\/SQL-Server-Restore-DateTime.png\" width=\"493\" height=\"373\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2014\/03\/SQL-Server-Restore-DateTime.png 493w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2014\/03\/SQL-Server-Restore-DateTime-300x226.png 300w\" sizes=\"auto, (max-width: 493px) 100vw, 493px\" \/><\/a><\/p>\n<p>Let&#8217;s say our boss wants us to restore Monday&#8217;s production backup (Alabama) every Friday afternoon to our development database (Tide). To accomplish this task, we can use the built-in SQL Server DateTime functions.<\/p>\n<p>The below script will restore from the backup created on the <strong>first day of the current week.<\/strong>\u00a0 I&#8217;ve added comments to explain the code.<\/p>\n<div>\n<pre>-- Declare variables\r\nDECLARE @backup nvarchar(200) \r\nDECLARE @datebegin datetime\r\nDECLARE @dateend datetime\r\n\r\n-- Initalize variables\r\n-- Set @datebegin equal to the first day of the current week\r\nSELECT @datebegin = DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0)\r\n-- Set @dateend equal to the second day of the current week \r\nSELECT @dateend = DATEADD(wk,DATEDIFF(wk,0,GETDATE()),1) \r\n\r\n-- Set @backup equal to query dependent on datebegin and dateend \r\nSELECT TOP 1 @backup = name + '.bak' \r\nFROM msdb..backupset \r\nWHERE database_name = 'Alabama' \r\nAND backup_start_date BETWEEN @datebegin AND @dateend \r\nAND type = 'D' -- D is for full backups\r\nORDER BY backup_start_date ASC \r\n\r\nUSE [master]\r\n\r\n-- Put DB in Single_User Mode\r\nALTER DATABASE [Tide] SET SINGLE_USER WITH ROLLBACK IMMEDIATE \r\n\r\n-- Restore DB using query from @backup variable\r\nRESTORE DATABASE [Tide] FROM  DISK = @backup WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5 \r\nGO<\/pre>\n<\/div>\n<p>Below is a table of useful DateTime functions that you can use for the @datebegin and @dateend variables.<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td><strong>Day<\/strong><\/td>\n<td><strong>SQL<\/strong><\/td>\n<\/tr>\n<tr>\n<td>Today<\/td>\n<td>SELECT GETDATE()<\/td>\n<\/tr>\n<tr>\n<td>Yesterday<\/td>\n<td>SELECT DATEADD(d, -1, GETDATE())<\/td>\n<\/tr>\n<tr>\n<td>First Day of Current Week<\/td>\n<td>SELECT DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0)<\/td>\n<\/tr>\n<tr>\n<td>Last Day of the Current Week<\/td>\n<td>SELECT DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 6)<\/td>\n<\/tr>\n<tr>\n<td>First Day of the Current Month<\/td>\n<td>SELECT DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)<\/td>\n<\/tr>\n<tr>\n<td>Last Day of the Current Month<\/td>\n<td>SELECT DATEADD(ms,- 3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE())+1,0)))<\/td>\n<\/tr>\n<tr>\n<td>First Day of the Current Year<\/td>\n<td>SELECT DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)<\/td>\n<\/tr>\n<tr>\n<td>Last Day of the Current Year<\/td>\n<td>SELECT DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,GETDATE())+1,0)))<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Another example may include where you need to take a backup from the first of the month of the production database and restore it weekly to the development database. In this situation you can edit the @datebegin and @dateend variables:<\/p>\n<pre>--Set @datebegin equal to the first day of the current month\r\nSELECT @datebegin = DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0) \r\n\r\n--Set @dateend equal to the second day of the current month\r\nSELECT @dateend = SELECT DATEADD(mm,DATEDIFF(mm,0,GETDATE()),1)<\/pre>\n<p><a href=\"http:\/\/www.mssqltips.com\/sqlservertip\/2835\/restore-sql-server-databases-using-datetime-functions\/\" target=\"_blank\">Click here to view the rest of this post.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you take full backups using SQL Server maintenance plans and let SQL Server use the default naming convention, you have probably noticed that usually you&#8217;ll have file name in the format of &#8220;database name + backup + date + time + .bak&#8221;. For example, a backup from the master database may look like this: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[104],"tags":[14,59,62,40],"class_list":["post-256","post","type-post","status-publish","format-standard","hentry","category-backuprestore","tag-backuprestore","tag-development","tag-functions","tag-tsql"],"_links":{"self":[{"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/comments?post=256"}],"version-history":[{"count":1,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/256\/revisions"}],"predecessor-version":[{"id":258,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/256\/revisions\/258"}],"wp:attachment":[{"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/media?parent=256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/categories?post=256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/tags?post=256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}