CASE input_expression WHEN when_expression THEN result_expression [ ...n ] [ELSE else_result_expression] END
CASE WHEN column1 = 'yes' OR column2 = 'other' THEN 'yes/other' END
--example 1 CASE WHEN DateDiff(d, DateValue, GetDate())>0 THEN 'past' WHEN DateDiff(d, DateValue, GetDate())=0 THEN 'today' ELSE 'future' END --example 2 CASE WHEN column1 BETWEEN 1 AND 100 THEN '1st range' WHEN column1 BETWEEN 101 AND 1000 THEN '2nd range' ELSE '3rd range' END
--example 3 CASE WHEN column1 IN ('A', 'B', 'C') THEN '1st set' WHEN column1 IN ('C', 'D', 'E') THEN '2nd set' ELSE '3rd set' END
IsNull(expression)
-- example for string values SELECT * FROM table1 A LEFT JOIN table2 B ON A.column1 = B.column1 AND IsNull(A.column2, ' ') = IsNull(B.column2, ' ') WHERE B.column1 IS NULL -- example for numeric values SELECT * FROM table1 A LEFT JOIN table2 B ON A.column1 = B.column1 AND IsNull(A.column2, 0) = IsNull(B.column2, 0) WHERE B.column1 IS NULL
COALESCE ( expression [ ,...n ] )
-- example coalesce SELECT * FROM table1 WHERE COALESCE(Column1, Column2, Column3) IS NOT NULL -- example IsNull alternative SELECT * FROM table1 WHERE Column1 IS NOT NULL OR Column2 IS NOT NULL OR Column3 IS NOT NULL
NullIf ( expression , expression )
-- example numeric values SELECT column1 , NullIf(column2, 0) column2 FROM table_name -- example string or date values SELECT column1 , NullIf(column2, '') column2 FROM table_name
-- example multiple checks SELECT column1 , CASE WHEN column2 IN ('A', 'B', 'V') THEN NULL ELSE column1 END FROM table_name -- alternative SELECT column1, NullIf(NullIf(NullIf(column2, 'A'), 'B'), 'V') FROM table_name
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
SELECT CONVERT(varchar(10), GetDate(),101) -- US format: mm/dd/yyyy SELECT CONVERT(varchar(10), GetDate(),102) -- ANSI format: yyyy.mm.dd SELECT CONVERT(varchar(10), GetDate(),103) -- British/French dd/mm/yyyy SELECT CONVERT(varchar(10), GetDate(),104) -- German format: dd.mm.yyyy SELECT CONVERT(varchar(12), GetDate(),105) -- Italian format: dd-mm-yyyy SELECT CONVERT(varchar(12), GetDate(),106) -- dd mon yyyy SELECT Upper(CONVERT(varchar(12), GetDate(),106)) -- upper case of: dd mon yyyy SELECT CONVERT(varchar(8), GetDate(),112) -- ISO format (preferred): yyyymmdd SELECT CONVERT(varchar(10), GetDate(), 23) -- yyyy-mm-dd SELECT CONVERT(varchar(23), GetDate(),126) -- yyyy-mm-ddThh:mi:ss.mmm
SELECT CAST(column1 as decimal(12,2)) + 101 FROM table_name WHERE IsNumeric(column1)=1
-- example SELECT CAST(column1 as decimal(12,2)) + 101 FROM table_name WHERE column2 = 'value' AND IsNumeric(column1)=1 -- alternative writing (recommended) SELECT CAST(column1 as decimal(12,2)) + 101 FROM table_name WHERE IsNumeric(column1)=1 AND column2 = 'value'
IsDate( expression )
SELECT IsDate(GetDate()) SELECT IsDate('11/31/2007') --november has only 30 days SELECT CASE IsDate('11/31/2007') WHEN 1 THEN 'Yes' ELSE 'No' END
For more information on the function is always a good idea to consult Microsoft Technical documentation.