Results 1 to 6 of 6

Thread: sql code help

  1. #1
    Join Date
    May 2013
    Posts
    2

    Question Unanswered: sql code help

    HI all,

    Need to know if this is possible...

    I need to select a field depeneding on the value of another field.
    Example:
    Table has fields: ri.cnt, ri.date_1 thru ri.date_99, and other fields.
    If the value of ri.cnt = 50, I need to select ri.date_48 (ri.cnt - 2). Is there a way to write this sql code?

    The best I could do was:
    'ri.ridate_'||cast(ri.ricnt-2 as char(2))

    but this only yields ri.ridate_48 as text - not the actual field value.

    TIA,
    JS-R

  2. #2
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5
    Look at the CASE expression.

    Andy

  3. #3
    Join Date
    May 2013
    Posts
    2
    Andy,

    Thanks for the quick response!

    If I use CASE, I will need 99 When clauses, correct:
    CASE
    When ri.ricnt = 3 then ri.ridate_1
    When ri.ricnt = 4 then ri.ridate_2
    When ri.ricnt = 5 then ri.ridate_3
    .
    .
    .
    When ri.ricnt = 101 then ri.ridate_99
    END

    Is this the easiest way to handle this?

  4. #4
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5
    I think so.

    Andy

  5. #5
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    Quote Originally Posted by js-r View Post
    If I use CASE, I will need 99 When clauses, correct:
    ...
    Is this the easiest way to handle this?
    I'm sure the person who came up with this data model would have a good laugh.
    ---
    "It does not work" is not a valid problem statement.

  6. #6
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Quote Originally Posted by n_i View Post
    I'm sure the person who came up with this data model would have a good laugh.
    I would be more inclined to guess a maniacal laugh, the kind that you sometimes hear in old horror movies... I bet that the laugh still happens whenever they think about people having to work with it too!

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

Posting Permissions

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