Wednesday, October 22, 2008

CASE SQL

-- CASE NULL --

-- Example 1:
CASE WHEN ss.SessionID IS NULL THEN 'No' ELSE 'Yes' END AS IsActive

-- Example 2:
(CASE WHEN v.[Name] is null then '' else v.[Name] end) as SomeName

-- Example 3:
SELECT g.GraphID,
g.Description,
u.UserGraphID,
CASE WHEN u.GraphTypeID IS NULL AND g.GraphID = 7 THEN 2 ELSE u.GraphTypeID END AS GraphTypeID,
u.IsActive

FROM Graphs g
LEFT JOIN UserGraphs u on (g.GraphID = u.GraphID)

Example 4

SELECT ID,

CASE TypeID

WHEN 0 THEN 1 -- Standard

WHEN 2 THEN 2 -- Enterprise

WHEN 1 THEN 3 -- Premium

ELSE 0 -- Unknown

END AS TheType

FROM Users

No comments: