SQL Cheatsheet

Multiple “Order By” Clause

Separate the order by clauses with commas.

SELECT ...
  WHERE ...
  ORDER BY ID desc, ReferenceNumber

Use “CASE” to Convert Non-String to String Value

In this example, we want do display the SettlementType value (an integer) as a human readable value in a column named “Settlement”.

SELECT [Number],
  CASE WHEN [SettlementType] = 0 THEN 'CDF'
    WHEN [SettlementType] = 1 THEN 'HUD1'
    WHEN [SettlementType] = 2 THEN 'CSS'
  END as 'Settlement'
FROM ...

Resulting SQL view.

Update Row Value

The basic command is:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition

For instance:

UPDATE [dbo].[Lookup]
SET IsSystem = 0, IsActive = 1
WHERE Name like '9fc5c91e-ea03-43a1-a3e4-d0d0d698e31c'