Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL SELECT - Return boolean based on condition

Essentially I'm trying to do this

select  u.Hostname, u.IsCustom, (u.Status = 5) as IsActive from    SiteUrlMappings u 

Where 5 is an int representing an "Active" url.

Of course this doesn't work, and my sql is rusty like an old screwdriver.

like image 758
Ben Foster Avatar asked Oct 15 '11 14:10

Ben Foster


1 Answers

You don't need a CASE expression
Just leverage how bit works: all non-zero values give 1 when cast to bit

SELECT     u.Hostname,     u.IsCustom,     ~ CAST((u.Status - 5) AS bit) AS IsActive from    SiteUrlMappings u 
like image 178
gbn Avatar answered Sep 24 '22 03:09

gbn