Saturday, July 9, 2022

[FIXED] What is the 'when keyword' used for in T-SQL?

Issue

What is the when keyword used for in T-SQL?

when

NOTE: I tried searching this on the web (e.g. 'Googling')... however due to the ubiquitous nature of the word 'when', I wasn't able to find a good explanation.

Furthermore, a list of SQL keywords did not include 'when' so either the list was not exhaustive or it is unique to T-SQL (or perhaps it was added in some 'newer' version of T-SQL / SSMS). Link to this particular SQL keyword site: https://www.w3schools.com/sql/sql_ref_keywords.asp


Solution

It's used in conjunction with the CASE keyword, which is like a switch, or 'if' statement essentially... for example:

SELECT 
    CASE WHEN [Column] = 1 THEN 'Column is 1'
         WHEN [Column] = 2 THEN 'Column is 2'
         ELSE 'Column is not 1 or 2'
         END AS [Description]


Answered By - Milney
Answer Checked By - Marilyn (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.