Professional Web Applications Themes

CFQUERY with IF THEN ELSE - Coldfusion Database Access

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 simple query like <CFQUERY>SELECT * FROM XYZ WHERE Date100 = '01/01/2005' and now here is what I would like to add to it: IF (Date800 = '' or Date800 IS NULL) AND PlanYrEnd <= #CreateODBCDate(DateAdd('d',75,Back5Months))# ELSE AND PlanYrEnd <= #CreateODBCDate(Back5Months)# END Note: Date800 and PlanYrEnd are columns on my SQL 2000 table and Back5Months is a ColdFusion Date Variable. If a the first statement is true I would like to ...

  1. #1

    Default 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 simple
    query like <CFQUERY>SELECT * FROM XYZ WHERE Date100 = '01/01/2005' and now here
    is what I would like to add to it: IF (Date800 = '' or Date800 IS NULL) AND
    PlanYrEnd <= #CreateODBCDate(DateAdd('d',75,Back5Months))# ELSE AND PlanYrEnd
    <= #CreateODBCDate(Back5Months)# END Note: Date800 and PlanYrEnd are columns
    on my SQL 2000 table and Back5Months is a ColdFusion Date Variable. If a the
    first statement is true I would like to add 'AND PlanYrEnd <=
    #CreateODBCDate(DateAdd('d',75,Back5Months))#' to my WHERE Clause and if it is
    not true then I would like to add 'AND PlanYrEnd <=
    #CreateODBCDate(Back5Months)#' to my WHERE Clause. I don't know how to do
    this. Some on different told me about using Case Statements but I had no clue
    what they were talking about. I would really appriciate if someone can help me
    to rewrite this query. Thanks, Mike:Q

    mike2004 Guest

  2. #2

    Default Re: CFQUERY with IF THEN ELSE

    Is this closer to what you had in mind?

    Phil



    SELECT *
    FROM XYZ
    WHERE Date100 = '01/01/2005'
    AND (((Date800 = '' OR Date800 IS NULL) AND PlanYrEnd <=
    #CreateODBCDate(DateAdd("d",75,Back5Months))#)
    OR PlanYrEnd <= #CreateODBCDate(Back5Months)#)

    paross1 Guest

  3. #3

    Default Re: CFQUERY with IF THEN ELSE

    Thanks paross1 for your reply. I tried your query and the output is still
    showing all the extra data which I don't wanna show! Please advice. I'm sure
    we both are in the right direction but need to work together to finalize the
    codes.

    mike2004 Guest

  4. #4

    Default Re: CFQUERY with IF THEN ELSE

    I didn't think this version out too much, but you get the idea....

    Phil



    SELECT *
    FROM XYZ
    WHERE Date100 = '01/01/2005'
    AND (((Date800 = '' OR Date800 IS NULL) AND PlanYrEnd <=
    #CreateODBCDate(DateAdd("d",75,Back5Months))#)
    OR ((Date800 <> '' OR Date800 IS NOT NULL) AND PlanYrEnd <=
    #CreateODBCDate(Back5Months)#))

    paross1 Guest

  5. #5

    Default Re: CFQUERY with IF THEN ELSE

    No Luck!!! Now I am even getting more outputs.
    mike2004 Guest

  6. #6

    Default Re: CFQUERY with IF THEN ELSE

    Which records are you seeing that you do not expect? In other words, what are the values of Date100, Date800, PlanYrEnd, and Back5Months for the "offending" rows?

    Phil
    paross1 Guest

  7. #7

    Default Re: CFQUERY with IF THEN ELSE

    Bsically I would like to add 75 Days to Variable Back5Month if there's a date in field DATE800. This has to apply to each row so the only way I can this is to include this in the QUERY.
    mike2004 Guest

  8. #8

    Default Re: CFQUERY with IF THEN ELSE

    What am I missing? This seems to me like it would give you what you are trying
    to get, unless my brain just isn't working logically today.

    Phil

    SELECT *
    FROM XYZ
    WHERE Date100 = '01/01/2005'
    AND PlanYrEnd =
    CASE
    WHEN (Date800 = '' OR Date800 IS NULL) THEN
    #CreateODBCDate(DateAdd("d",75,Back5Months))#
    WHEN (Date800 <> '' AND Date800 IS NOT NULL) THEN
    #CreateODBCDate(Back5Months)#
    END

    paross1 Guest

  9. #9

    Default Re: CFQUERY with IF THEN ELSE

    Now I am getting syntax error:

    WHERE ClientDB.RecNum = AnnualDB.ClientId
    AND PlanDB.ClientId = AnnualDB.ClientId
    AND PlanDB.RecNum = AnnualDB.PlanId
    AND PlanStatusId < '3'
    AND (((Date805 IS NULL OR Date806 = '') AND (Chk805 = '0' OR Chk805 IS NULL
    OR Chk805 = '')) OR ((Date806 IS NULL OR Date806 = '') AND (Chk806 = '0' OR
    Chk806 IS NULL OR Chk806 = '')))
    AND PlanYrEnd <= #CreateODBCDate(EndOfThisMonth)#
    AND PlanYrEnd >= #CreateODBCDate(Back11Months)#
    CASE
    WHEN Date800 = '' OR Date800 IS NULL THEN
    #CreateODBCDate(DateAdd("d",75,Back5Months))#
    ELSE #CreateODBCDate(Back5Months)#
    END

    Order by Month(PlanYrEnd), CustNum asc

    mike2004 Guest

  10. #10

    Default Re: CFQUERY with IF THEN ELSE

    You forgot the AND PlanYrEnd <= CASE.... part. By the way, I am assuming SQL
    Server DB.

    Phil



    WHERE ClientDB.RecNum = AnnualDB.ClientId
    AND PlanDB.ClientId = AnnualDB.ClientId
    AND PlanDB.RecNum = AnnualDB.PlanId
    AND PlanStatusId < '3'
    AND (((Date805 IS NULL OR Date806 = '') AND (Chk805 = '0' OR Chk805 IS NULL OR
    Chk805 = '')) OR ((Date806 IS NULL OR Date806 = '') AND (Chk806 = '0' OR Chk806
    IS NULL OR Chk806 = '')))
    AND PlanYrEnd <= #CreateODBCDate(EndOfThisMonth)#
    AND PlanYrEnd >= #CreateODBCDate(Back11Months)#
    AND PlanYrEnd <=
    CASE
    WHEN Date800 = '' OR Date800 IS NULL THEN
    #CreateODBCDate(DateAdd("d",75,Back5Months))#
    ELSE #CreateODBCDate(Back5Months)#
    END
    Order by Month(PlanYrEnd), CustNum asc

    paross1 Guest

  11. #11

    Default Re: CFQUERY with IF THEN ELSE

    Perfect. Thanks a lot.

    Mike :beer;
    mike2004 Guest

Similar Threads

  1. cfquery bug...still?
    By jlerath in forum Coldfusion Server Administration
    Replies: 2
    Last Post: November 30th, 06:06 PM
  2. CFQuery issue
    By colesc8 in forum Coldfusion - Getting Started
    Replies: 2
    Last Post: September 9th, 03:47 PM
  3. cfquery
    By ducman in forum Macromedia ColdFusion
    Replies: 3
    Last Post: August 2nd, 07:09 AM
  4. Output from Cfquery
    By mike in forum Coldfusion Database Access
    Replies: 0
    Last Post: March 29th, 04:52 PM
  5. need help with cfquery
    By mkauspe in forum Coldfusion Database Access
    Replies: 3
    Last Post: March 24th, 11:11 PM

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
  •  

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