Sample Header Ad - 728x90

Does SQL Server CASE statement evaluate all conditions or exit on first TRUE condition?

66 votes
4 answers
149168 views
Does the SQL Server (2008 or 2012, specifically) CASE statement evaluate all the WHEN conditions or does it exit once it finds a WHEN clause that evaluates to true? If it does go through the entire set of conditions, does that mean that the last condition evaluating to true overwrites what the first condition that evaluated to true did? For example: SELECT CASE WHEN 1+1 = 2 THEN'YES' WHEN 1+1 = 3 THEN 'NO' WHEN 1+1 = 2 THEN 'NO' END The results is "YES" even though the last when condition should make it evaluate to "NO". It seems that it exits once it finds the first TRUE condition. Can someone please confirm if this is the *case*.
Asked by Juan Velez (3303 rep)
May 29, 2013, 06:33 PM
Last activity: Jun 23, 2025, 02:24 PM