The CASE expression in UPDATE statement in SQL Server 2012 – part 3

In this post You will be continue your adventure with CASE expression in SQL Server 2012.


First sep is to create new table TabPerson , in it we put several data from Person table.

USE AdventureWorks2012;
GO
SELECT [Title], [FirstName], [LastName]
       ,YEAR([ModifiedDate]) as year
INTO TabPerson
FROM [AdventureWorks2012].[Person].[Person]
WHERE [Title] is NOT NULL 
AND [LastName] LIKE 'Va%'

Then we check which data are in new table:

USE AdventureWorks2012;
GO
SELECT Title, FirstName, LastName ,year
FROM TabPerson;

gg1

We update year column in this table, change value of 4-lettters year to 1-letters value using CASE clause:

USE AdventureWorks2012;
GO
UPDATE TabPerson
SET year = ( CASE year
             WHEN 2005 THEN 5
	     WHEN 2006 THEN 6
	     ELSE 7
             END
           );

And we check data in TabPerson table:

USE AdventureWorks2012;
GO
SELECT Title, FirstName, LastName ,year
FROM TabPerson;

gg2

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.