MSSQL IF (multiple fields > one value)

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.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

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