Random Awesomeness 2008-02-19

Posted by Ikhwan on February 19, 2008

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.

Comments

Leave a response

  1. Amry Wed, 20 Feb 2008 08:21:55 MYT

    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

  2. Ikhwan Wed, 20 Feb 2008 08:52:42 MYT

    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.

Comments

*
To prove you're a person (not a spam script), type the security word shown in the picture. You can register to this site so you won't be asked to do this again. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word