T-SQL – Using CASE In An Update Statement

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s