Random Awesomeness 2008-02-19
Take a look at this, columnName is a bit data type, 1 or 0.
SELECT SUBSTRING('YesNo', 4 - 3 * columnName, 3) as YesNo
FROM TableName
Neat huh? Although I prefer to collect real data from database and do formatting in the client code, this is a sweet hack nevertheless. Got it from this blog entry.
— UPDATE —
This is (somewhat) the equivalent in C#.
Console.WriteLine("YesNo".Substring(3 - 3 * Convert.ToInt16(boolValue),
2 + Convert.ToInt16(boolValue)));
Or… You can just do this instead :P
Console.WriteLine(boolValue ? "Yes" : "No");
Trackbacks
Trackbacks are closed.


Prefer
SELECT CASE columnName WHEN 1 THEN ‘Yes’ ELSE ‘No’ END AS YesNo FROM TableName
Longer SQL, yes, but less cryptic. Equivalent to the
boolValue ? “Yes” : “No”
statement.
p/s: Ruang komen ni takde tag untuk buat code box macam di atas tu ke, cantik la.. hehe :D
Yeah, the CASE is more clear. Too bad SQL Server’s T-SQL doesn’t have IF() like MySQL.
SELECT IF(ColumnName IS TRUE, “Yes”, “No”) FROM TableName
T-SQL is lagging behind in many ways than others like MySQL, especially when compared to Oracle and PostgreSQL.