Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2004
    Posts
    15

    Unanswered: Conditional Select Statement

    Hello dbForumers,

    Yet another puzzling question. I remember I saw somewhere a particular syntax to select a column based on a conditional predicate w/o using a user defined function. What I want to accomplish is this : SELECT (if column colA is empty then colB else colA) as colC from SomeTable. Possible ? Not possible? Have I hallucinated ?

    Thank You!

  2. #2
    Join Date
    Mar 2004
    Posts
    80

    Re: Conditional Select Statement

    possible.
    select (case colA when ='' then colB else colA end) as colC

    Originally posted by Rollmops
    Hello dbForumers,

    Yet another puzzling question. I remember I saw somewhere a particular syntax to select a column based on a conditional predicate w/o using a user defined function. What I want to accomplish is this : SELECT (if column colA is empty then colB else colA) as colC from SomeTable. Possible ? Not possible? Have I hallucinated ?

    Thank You!

  3. #3
    Join Date
    Mar 2004
    Posts
    15
    Yay, right on target.

    But now I have some difficulties testing the NULL state... the syntax: ...(CASE VTE1 WHEN NULL THEN ACHN ELSE VTE1 END) AS COND_ACHN... won't throw any errors but wont work as excepted since it always sends the ELSE case no matter what...

  4. #4
    Join Date
    Mar 2004
    Posts
    80
    select isnull(vte1,achn) as COND_ACHN
    or

    select (CASE WHEN VTE1 is NULL THEN ACHN ELSE VTE1 END) AS COND_ACHN

    Originally posted by Rollmops
    Yay, right on target.

    But now I have some difficulties testing the NULL state... the syntax: ...(CASE VTE1 WHEN NULL THEN ACHN ELSE VTE1 END) AS COND_ACHN... won't throw any errors but wont work as excepted since it always sends the ELSE case no matter what...
    Last edited by theguru; 03-23-04 at 05:31.

  5. #5
    Join Date
    Mar 2004
    Posts
    15
    Yay, right on target.

    But now I have some difficulties testing the NULL state... the syntax: ...(CASE VTE1 WHEN NULL THEN ACHN ELSE VTE1 END) AS COND_ACHN... won't throw any errors but wont work as excepted since it always sends the ELSE case no matter what...

  6. #6
    Join Date
    Mar 2004
    Posts
    80
    To determine if an expression is NULL, use IS NULL or IS NOT NULL rather than comparison operators (such as = or !=).
    follow the code of my previous message.It should work for u.

    Originally posted by Rollmops
    Yay, right on target.

    But now I have some difficulties testing the NULL state... the syntax: ...(CASE VTE1 WHEN NULL THEN ACHN ELSE VTE1 END) AS COND_ACHN... won't throw any errors but wont work as excepted since it always sends the ELSE case no matter what...

  7. #7
    Join Date
    Mar 2004
    Posts
    15

    Thumbs up

    I just had to remove the 'VTE1' in ...(CASE VTE1... for the predicate to work accordingly =) anyways thanks a lot it works just fine now =)

Posting Permissions

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