Archive

Posts Tagged ‘update’

T-SQL – Using CASE In An Update Statement

February 19, 2009 Leave a comment

The following example will update [TABLE] and assign a value to the PRICEBRACKET field based on the value of the pice field:

UPDATE [TABLE]
SET PRICEBRACKET =
CASE
WHEN (price < 5000)
THEN ‘Less Than 5K’
WHEN (price >= 5000 AND price <10000)
THEN ‘Between 5K and 10K’
WHEN (price >= 10000 AND price <20000)
THEN ‘Between 10K and 20K’
WHEN (price >= 20000 AND price <50000)
THEN ‘Between 20K and 50K’
ELSE ‘Above 50K’
END

Categories: SQL Tags: , , , ,

SQL – Joined Update – Table update With A Join

November 4, 2008 Leave a comment

 

The following joined update updates a table called data.dbo.table1 from a table called data.dbo.table2 and sets the fielda in table1 equal to fielda in table2 based on a matching ref:

updatedata.dbo.table1

      setfielda = b.fielda

fromdata.dbo.table1 as a

joindata.dbo.table2 as b

on a.ref = b.ref

Categories: SQL Tags: , , ,
Follow

Get every new post delivered to your Inbox.