+ Reply to Thread
Results 1 to 3 of 3

Thread: SQL Decision Structures

  1. #1
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    SQL Decision Structures

    SQL Decision Structures

    If you have programmed before, then these will be familiar. The two control structures available are: CASE and IF.

    If... Else

    These functions return a value depending on whether a condition is true or not.

    Think of it like this:

    Code:
    if ((x == x) == true) {
    	return "X"
    } else {
    	// false
    	return "Y";
    }
    The generic syntax for this function is:

    Code:
    IF (expr,val1,val2)
    The expression that we are testing is the first parameter. If expr is true, then the return value is val, otherwise the return value is val2.

    Example:

    Code:
    SELECT IF(5>2,'a',4)
    If 5 is greater than 2, the return value is 'a'. If 5 is less than 2 the return value is 4. Since 5 is greater than 2, the return value is 'a'.

    Another example:

    Code:
    SELECT IF(3>4,'greater','less')
    Result:

    less
    You can nest if structures by including an if structure as one of the return values. If you have used Excel before then this notation will be familar to you. You can make more complicated statements using AND and or operators.

    This might be useful when you want to return data from one table, if a certain condition is true or from a different table if the condition is false.

    Switch Case

    The switch case structure is a concise method of testing one variable against multiple values.

    The generic syntax is:

    Code:
    SELECT CASE value WHEN compareValue THEN result WHEN compare2 THEN result ELSE result END;
    Example:

    Code:
    SELECT CASE 1 WHEN 5 THEN '5' WHEN 4 THEN '4' WHEN 3 THEN '3' ELSE 1 END;
    Output:

    1
    This is a useless example but you might want to test a value that the user has entered and return a certain value depending on what the value is, or insert it into a different table depending on the value.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: SQL Decision Structures

    CASE can be really handy with metadata with certain types of databases. Some of them represent things like VARCHAR as odd numeric codes internally. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Jordan Guest

    Re: SQL Decision Structures

    I've rarely used if/else and I don't think I even knew case structures existed in SQL! Nice one, +rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. how do i make the decision work
    By atoivan in forum C and C++
    Replies: 4
    Last Post: 06-20-2011, 12:59 PM
  2. Netbook vs. Nettop - final decision!
    By AdvMutant in forum The Lounge
    Replies: 5
    Last Post: 11-27-2010, 06:30 AM
  3. This hostinyg review helps all of you in taking a decision
    By olivia in forum Hosting and Registrars
    Replies: 2
    Last Post: 05-28-2009, 10:21 AM
  4. Destruction & Last Decision
    By Jaan in forum The Lounge
    Replies: 13
    Last Post: 08-21-2008, 03:59 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts