In MySQL I use IF(value,then,else) quite often, but in a new project I am doing I need to do the same in MSSQL.

SELECT field1, field2, IF(field3 = '1', 'Started', 'Not Started') AS status FROM table WHERE id = x;

This won’t work in MSSQL, but this will:

SELECT field1, field2, status = (CASE WHEN field3 = '1' THEN 'Started' WHEN field3 <> '1' THEN 'Not Started' END) FROM table WHERE id = x;

Not worked out the case ELSE yet, but haven’t got time today, so just recording the above.

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.