SQL: Rename a database with TSQL

You can rename a SQL database using the SQL Server Management Studio (SSMS), however there may be times that you do not have access SSMS or would like to script the task.

The following TSQL will rename a SQL Database:

 

ALTER DATABASE <database name> SET SINGLE_USER  WITH ROLLBACK IMMEDIATE
 ALTER DATABASE <database name> MODIFY NAME = <new database name>
ALTER DATABASE <new database name> SET MULTI_USER

 

Replace the and values with your database names. Note that the database must be in SINGLE_USER mode in order to complete this ALTER DATABASE command. In the code above, the set option WITH ROLLBACK IMMEDIATE tells the SQL Server that if it can’t complete the command right away, then the other pending transactions should be rolled back. This is helpful if there are automatic connections being made to the database that are difficult to KILL.

The following script can also be used to rename a database:

 

use [master]
  declare @olddb sysname
  declare @newdb sysname
  declare @cmd varchar(max)
  
  set @olddb = 'OldDbName' -- set the existing database name here
  set @newdb = 'NewDBName' -- set the new database name here
  
  set @cmd = 'ALTER DATABASE ' + @olddb + ' SET SINGLE_USER' -- you can add options to the command
  print @cmd
  exec (@cmd)
  
  set @cmd = 'ALTER DATABASE ' + @olddb + ' MODIFY NAME = ' + @newdb
  print @cmd
  exec (@cmd)
  
  set @cmd = 'ALTER DATABASE ' + @newdb + ' SET multi_user'
  print @cmd
  exec (@cmd)



   

C#: Capture a ScreenShot

The following code demonstrates one method of capturing a screenshot. The method displayed returns a screenshot of the entire screen as a bitmap.

 

private Bitmap GetScreenShot()
        {
            // set screenshot bounds to match the size of the screen
            Rectangle bounds = Screen.PrimaryScreen.Bounds;
            // crate a bitmap with the dimensions of the sreen
            Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy);
            }
            return bitmap;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = GetScreenShot();
        }



   

SQL: First and Last Day of Last Year

The following SQL code is an example of retrieving the first day and the last day of last year:
 
-- first day of last year
SELECT DATEADD(yy, DATEDIFF(yy,0,GETDATE())-1, 0) 

-- last day of last year
SELECT DATEADD(dd,-1,DATEADD(yy, DATEDIFF(yy,0,GETDATE()),0))

 

 



   

SQL: First and Last Day of Next Year

The following SQL code is an example of retrieving the first day and the last day of next year:
-- first day of next year
SELECT DATEADD(yy, DATEDIFF(yy,0,GETDATE())+1,0) 

-- last day of next year
SELECT DATEADD(dd,-1,DATEADD(yy, DATEDIFF(yy,0,GETDATE())+2,0))

 

 

 



   

SQL: First and Last Day of Next Month

The following SQL code is an example of retrieving the first day and the last day of next month:
 
-- first day of next month
SELECT dateadd(mm,0,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))

-- last day of next month
SELECT dateadd(dd,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+2,0))