sql server - CASE WHEN bit THEN 'something' ELSE null END shortcut -


is there shortcut in ms sql server call case when bit 'something' else null end ? there way write differently?

example scenario:

declare @data table(issomething bit not null, value nvarchar(30) not null);  insert @data values (1, 'a'), (1, 'b'), (0, 'c'), (0, 'd');  select case when issomething = 1 value else null end, case when issomething = 1 'string' else null end, case when issomething = 0 'not ' + value else null end @data 

i looking variant bit value 1 (true).

the concern readability of select statement , excesively long expression write simple case/if.

i use expressions in 1 of views bothers me most. not saying design of view or tables ideal. (in fact, not ideal!)

you use iif (sql server 2012+) supposed shorthand case, in opinion query ends being less readable (might matter of habit on end though):

select     iif(issomething = 1, value1, null) val1,     iif(issomething = 1, value2, null) val2,     iif(issomething = 0, value3, null) val3 @data 

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -