Is there a shorter more terse way to do a string comparison in SQL Server without wrapping it in a CASE WHEN ... THEN 1 END
? In PostgreSQL, I can do
SELECT c, c='foo'
FROM ( VALUES ('foo'),('bar') ) AS t(c);
In SQL Server, I have to do
SELECT c, CASE WHEN c='foo' THEN 1 END
FROM ( VALUES ('foo'),('bar') ) AS t(c);
In the abstract, is there an easier more SQL-Servery way to throw an expression into boolean context? Or, is there a better way to write strcmp
without rolling your own case statement?
Best Answer
I'm not sure that you'd call it shorter, but you could use the Logical Function
IIF
. From the docs,Per the example,