Search code examples
t-sqlsql-server-2012

Adding a CASE statement has dramatically increased my query execution time


I have added the following CASE statement in my query:

CASE starlingYear
    WHEN  CONVERT(VARCHAR, 3000) THEN 'Not yet active'
ELSE CONVERT(VARCHAR, starlingYear)
END AS starBirth

Adding this CASE has increased the query execution time from less than a second to about 8 or 10 seconds.

The query only has about 1000 rows.

Is there a way to increase performance?


Solution

  • The syntax should be as follows. If startlingYear is 3000 (int) then 'Not yet active' else convert starlingYear to varchar.

    SELECT CASE  starlingYear
        WHEN 3000 
           THEN 'Not yet active'
        ELSE 
           CONVERT(VARCHAR(30), starlingYear) 
        END AS starBirth
    FROM table1