Thursday, May 1, 2014

Things that are convenient when your RDBMS has first class boolean support

Other RDBMSes could embed boolean expression anywhere:

select exists(select * from person) as has_record;

select 
  person_id in (1,6,8) as one_of_the_chosen_ones,
  person_id % 2 = 0 has_even_id,
  * 
from person;


You have to do this in SQL Server:
select 
  case when exists(select * from person) then 
    convert(bit, 1)
  else
    convert(bit, 0)
end as has_record;

select 
  case when person_id in (1,6,8) then
    convert(bit, 1)
  else
    convert(bit, 0)
  end as one_of_the_chosen_ones,
  case when person_id % 2 = 0 then 
    convert(bit, 1)
  else
    convert(bit, 0)
  end as has_even_id,
  * 
from person;

No comments:

Post a Comment