Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Using cfif in a where clause

New Here ,
Aug 27, 2013 Aug 27, 2013

Hi all,

This question is sort of related to a previous post of mine about querying dates.  I'm trying to use a cfif statement in my where clause to determine if a field is empty or not.  Here's the code:

<cfquery name="GetPastEvents" datasource="DSN">

     SELECT *

     FROM SITE:Calendar

     WHERE DatePart('yyyy', [StartDate]) = <cfqueryparam value="#Dateformat(Today, 'yyyy')#" cfsqltype="CF_SQL_DATE" />

     and <cfif EndDate neq ''>StartDate<cfelse>EndDate</cfif> < <cfqueryparam value="#Today#" cfsqltype="CF_SQL_DATE" />

     and Archive = <cfqueryparam value="0" cfsqltype="CF_SQL_INTEGER" />

     ORDER BY StartDate ASC, StartTime ASC

</cfquery>

My issue that that CF tells me EndDate is not defined.  EndDate is a field in the table SITE:Calendar.  There has to be a way to make this work, no?  Thanks!

1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Aug 27, 2013 Aug 27, 2013

You are mixing ColdFusion variables (which cfif requires) and SQL column names. Think of it this way, all CF tags and function execute before the SQL query is sent to the SQL server. You're mixing this and trying to run the CF statement on the SQL server and thus the failure.

You need something like this:

<cfquery name="GetPastEvents" datasource="DSN">

     SELECT *

     FROM SITE:Calendar

     WHERE DatePart('yyyy', [StartDate]) = <cfqueryparam value="#Dateformat(Today, 'yyyy')#" cfsqltype="CF_SQL_D

...
Translate
Advocate ,
Aug 27, 2013 Aug 27, 2013

You are mixing ColdFusion variables (which cfif requires) and SQL column names. Think of it this way, all CF tags and function execute before the SQL query is sent to the SQL server. You're mixing this and trying to run the CF statement on the SQL server and thus the failure.

You need something like this:

<cfquery name="GetPastEvents" datasource="DSN">

     SELECT *

     FROM SITE:Calendar

     WHERE DatePart('yyyy', [StartDate]) = <cfqueryparam value="#Dateformat(Today, 'yyyy')#" cfsqltype="CF_SQL_DATE" />

          and (

               (enddate is not null and EndDate < <cfqueryparam value="#Today#" cfsqltype="CF_SQL_DATE" />)

               or (enddate is null and StartDate < <cfqueryparam value="#Today#" cfsqltype="CF_SQL_DATE" />)

          )

          and Archive = <cfqueryparam value="0" cfsqltype="CF_SQL_INTEGER" />

     ORDER BY StartDate ASC, StartTime ASC

</cfquery>

Also, I think your cfif logic was backwards. Instead of NEQ I think you meant EQ because having an EndDate EQ '' and then comparing EndDate to Today (else clause) doesn't make sense.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 27, 2013 Aug 27, 2013
LATEST

Thank you again, Steve.  Your suggestions solved my problem!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources