Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2005
    Posts
    80

    Unanswered: specify IF CONDITION WITHIN SELECT

    Hi ,

    how to specify if condition within select statement??

  2. #2
    Join Date
    Jun 2006
    Posts
    471
    do you mean the where condition
    select .. from table.. where ..
    Is if not specific for sql in stored proc/functions
    Best Regards, Guy Przytula
    DB2 UDB LUW certified V6/7/8

  3. #3
    Join Date
    Jan 2005
    Posts
    80
    I mean a simple if condition. like this ( I know the syntax might not be right)
    select if ( emp_sal >1000) then emp_grade =500 from emp something like this

  4. #4
    Join Date
    Jan 2003
    Posts
    1,605
    select case when emp_sal > 1000 then 500 end from emp

    You can also use multiple case conditions and also ELSE case.

    Sample:
    select
    case
    when emp_sal > 1000 then 5000
    when emp_sal > 2000 then 4000
    else 0
    end
    from emp


    You can also use nested case statements.

    Sample:
    select
    case
    when emp_sal > 1000 then case when xxx = 3 then x else y end
    when emp_sal > 2000 then 4000
    else 0
    end
    from emp


    For more samples and any other SQL questions I recommend DB2 Cookbook - the best free SQL book ever seen: http://mysite.verizon.net/Graeme_Birchall/id1.html

    Hope this helps,
    Grofaty

  5. #5
    Join Date
    Jan 2005
    Posts
    80
    Thanks for the swift response!!!!

  6. #6
    Join Date
    Oct 2014
    Posts
    7
    Thank you for*good*communication.

  7. #7
    Join Date
    Dec 2007
    Location
    Richmond, VA
    Posts
    1,328
    Provided Answers: 5
    there is no IF in SQL, you have AND/OR conditions. Even though you can write an IF statement within an SQL procedure if you look at what comes out of that is just another AND/OR condtion in an SQL statement against the SYSDUMMY1 table.
    Dave

Posting Permissions

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