IF statement within CFQUERY/SQL

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default IF statement within CFQUERY/SQL

    Does anyone know if SQL has an IF statement? I can't seem to find an example
    that works.

    I know it is possible to perform a CFIF within a CFQUERY. However, I can't
    compare one of the DB fields to a variable or constant while within that
    CFQUERY.

    Here is some pseudo-code of what I am trying to do.

    SELECT type, category, description, datein
    FROM someTable
    WHERE type = 'K'
    IF (category = 5) THEN
    AND dateIn = 042105
    END IF

    None of the values in the WHERE statement are CF variables. Basically, I only
    want to check the dateIn value if the category field from the table equals 5.

    Is this possible?

    Thanks in advance for the help!!

    Unamailer Guest

  2. Similar Questions and Discussions

    1. Output from different datasource in cfquery statement
      i've make 2 cfquery statements. one refers to datasource DatStudent and another one refers to datasource DatModul. in coldfusion admin, DatStudent...
    2. Error executing a cfquery update statement. PLEASE HELP
      I have been over and over this. I am trying to update a simple table. I took this script from the same server using a different DSN. I keep...
    3. cfquery insert into sql statement
      Hello, I have a simple SQL statement (within a cfquery tag) that will not insert: <cfquery datasource="shoptrak" name="insert"> INSERT INTO...
    4. need help with cfquery
      This should be an easy one ... unfortunately I am a n00b to sql and CF. I am trying to display a certain column in a table, the most recent one. I...
    5. CFQUERY with IF THEN ELSE
      I have a CFQUERY which works perfectly and now I would like to add a little date calculation to this query to filter it little more. The query is a...
  3. #2

    Default Re: IF statement within CFQUERY/SQL

    I dont really know what you mean, but this may work?

    SELECT type, category, description, datein
    FROM someTable
    WHERE type = 'K'
    <CFIF (category eq 5) >
    AND dateIn = 042105
    </CFIF>

    mattis_72 Guest

  4. #3

    Default Re: IF statement within CFQUERY/SQL

    As far as I know you can't do this in pure sql. But you could do it in a
    stored procedure if your db allows them.

    But I think the following should return what you want.

    <cfquery>
    SELECT type, category, description, datein
    FROM someTable
    WHERE type = 'K'
    AND category <> = 5
    UNION
    SELECT type, category, description, datein
    FROM someTable
    WHERE type = 'K'
    AND category = 5
    AND dateIn = 042105
    </cfquery>


    Ken

    The ScareCrow Guest

  5. #4

    Default Re: IF statement within CFQUERY/SQL

    Unamailer wrote:
    > Does anyone know if SQL has an IF statement?
    CASE WHEN x THEN y ELSE z END

    > I know it is possible to perform a CFIF within a CFQUERY. However, I can't
    > compare one of the DB fields to a variable or constant while within that
    > CFQUERY.
    >
    > Here is some pseudo-code of what I am trying to do.
    >
    > SELECT type, category, description, datein
    > FROM someTable
    > WHERE type = 'K'
    > IF (category = 5) THEN
    > AND dateIn = 042105
    > END IF
    SELECT type, category, description, datein
    FROM someTable
    WHERE type = 'K' AND
    CASE WHEN category = 5 AND dateIn = 042105 THEN TRUE
    WHEN category = 5 THEN FALSE ELSE TRUE END

    Ugly, but should work.

    Jochem

    --
    Jochem van Dieten
    Team Macromedia Volunteer for ColdFusion, beer and fun.
    Jochem van Dieten - TMM Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139