1. We have moved to a new forum! There may be a few things not working properly so please let us know if you find a bug. Remember to use the bbCode [ sql ] tag for SQL statements.

How do i perform an If statement on a cloumn?

Discussion in 'SQL - Questions and Answers' started by Luke, May 24, 2006.

  1. Luke Guest

    Is is possibe to put an if statement on a column, for example, if a field in that column is empty then have it as false, if there is data in there then have it as true.
  2. ben Administrator

    Dialect: MySQL
    Yes you can do that.
    Here is an example that will check wether a colum "is_valid", which stores an integer value of 1 (for YES) or 0 for (NO), is valid or not and return the string YES or NO accordingly.

    SQL:

    SELECT
    id,
    IF(is_valid = 1, 'YES', 'NO') as is_valid
    FROM test_table


    In your case you just want to check if the a column (say "test_column") has a value or not so it will look something like:

    SQL:

    SELECT,
    id,
    IF ( test_column = '', 'FALSE', 'TRUE') as test_column
    FROM test_table


    The column "test_column" must be NOT NULL. If it allows NULLs you could check for that:

    SQL:

    IF(test_column IS NULL, 'FALSE', IF(test_column = '', 'FALSE', 'TRUE')) as test_column
  3. Dimitar Guest

    SQL:

    SELECT (CASE
    WHEN column is null THEN 1
    WHEN trim(column) = '' THEN 2
    ELSE 3
    END) as column_alias FROM table;

Share This Page