SQL Recipes
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread
  #1 (permalink)  
Old 05-24-2006, 08:14 PM
Luke
 
Posts: n/a
Default ANY dialect question:

How do i perform an If statement on a cloumn?


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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 05-25-2006, 12:48 AM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 95
ben has disabled reputation
Default MySQL answer. Re: How do i perform an If statement on a cloumn?

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.

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:

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:

IF(test_column IS NULL, 'FALSE', IF(test_column = '', 'FALSE', 'TRUE')) AS test_column
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-27-2006, 08:29 AM
Dimitar
 
Posts: n/a
Default ANY answer. Re: How do i perform an If statement on a cloumn?

SELECT (CASE
    WHEN COLUMN IS NULL THEN 1
    WHEN trim(COLUMN) = '' THEN 2
    ELSE 3
    END) AS column_alias FROM TABLE;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 12:20 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2008 SQL Recipes

1 2 3 4 5 6 7 8