How to pass a date into a ColdFusion collection to be searched

谁说我不能喝 提交于 2019-12-11 15:42:02

问题


I'm using a ColdFusion collection to search events and I need to pass a date into the collection as a "mmm" so it can be searched. Every time I try I get an error.

custom4="DateFormat(start_date, "mmm")"

Update: I'm trying to search "month" of the current year


回答1:


You should use the above code like

custom4=dateFormat(start_date, "mmm")

(Remove the outer double quotes)




回答2:


Quotes aren't the problem (and the suggestion of removing them actually causes an error). The problem is DateFormat() can't be applied to an entire query column. It's only capable of operating on a single value.

It would help to have more context about what you're trying to achieve, to determine the best approach.

  • If you want to find items dated in July of a specific year (i.e. July 2019) - then storing the full date, and searching for a date range, is probably a better way to go

  • If you want to find items dated in July of ANY year, then it's simpler to extract a month number within your SQL query, and store it in the collection. Then you need only search for a number.


Date Range Search

To search for a specific month/year, like July 2019, populate the collection with a timestamp field from your SQL query. Add the suffix _dt to the custom field name so it's treated as a date field

    cfindex( query="yourQuery"
            , collection="yourCollection"
            , action="Update"
            , type="Custom"
            , Start_Date_dt="yourTimeStampColumn"
            , ...
    );

In the search criteria, use the date range July 1 through August 1, 2019 (yes - August 1st). Dates must be formatted for Solr, which expects YYYY-MM-DDThh:mm:ssZ. NB: Dates should be in UTC (not local time).

cfsearch (name="searchResults"
    , collection="yourCollection"
    , criteria=' start_date_dt:[2019-07-01T04:00:00Z TO 2019-08-01T04:00:00Z}' 
);

Explanation/Notes

  • [ and } - Brackets indicates a range (i.e. from dateX TO dateY)
  • [ - Square bracket means inclusive (i.e. include July 1st)
  • } - Curly bracket means exclusive (i.e. exclude August 1st)
  • Field names should be in lower case

Month Number Search

To search for a specific month in any year, like July, extract the month number within your SQL query. (The exact syntax will be DBMS specific. You didn't mention which one you're using, so see your database's documentation on date functions.) Add the suffix _i to the custom field name so it's handled as an integer

    cfindex( query="yourQuery"
            , collection="yourCollection"
            , action="Update"
            , type="Custom"
            , monthNumber_i="theMonthNumberColumn"
            , ...
    );

Then simply search for the desired month number, i.e. 7 - July

cfsearch ( name="searchResults"
            , collection="yourCollection"
            , criteria=' monthnumber_i:7 '
    );

Full Example

Sample Query

sampleData = queryNew("MyID,Start_Date,MonthNumber"
    , "integer,timestamp,integer"
    , [{MyID=10, Start_Date="2019-06-30 12:30:00", MonthNumber=6}
    , {MyID=20, Start_Date="2019-07-01 00:00:00", MonthNumber=7}
    , {MyID=30, Start_Date="2019-07-01 16:30:00", MonthNumber=7}
    , {MyID=40, Start_Date="2019-07-31 23:50:00", MonthNumber=7}
    , {MyID=50, Start_Date="2019-08-01 00:00:00", MonthNumber=8}
]);

Create Collection

cfcollection ( action="create", collection="MyCollection");

Update Collection

cfindex( query="sampleData"
        , collection="MyCollection"
        , action="Update"
        , type="Custom"
        , key="MyID"
        , title="SampleData"
        , MonthNumber_i="MonthNumber"
        , Start_Date_dt="Start_Date"
        , body="MyID"
);

Find July, by month number

cfsearch ( name="monthNumberResults"
            , collection="MyCollection"
            , criteria=' monthnumber_i:7 '
        );

// results 
writeDump( var=monthNumberResults, label="Month Number Search" );   

Find July 2019, by date range

// Search range: July 1 to August 1, 2019
fromDate = "2019-07-01";
toDate   = dateAdd("m", 1, fromDate);

// Format dates for Solr
// Note: DateTimeFormat uses "n" for minutes. Valid in CF2016 Update 3 or higher
fromDate = dateTimeFormat( dateConvert("local2UTC", fromDate), "yyyy-mm-dd'T'HH:nn:ss'Z'");
toDate   = dateTimeFormat( dateConvert("local2UTC", toDate), "yyyy-mm-dd'T'HH:nn:ss'Z'");

cfsearch ( name="dateRangeResults"
            , collection="MyCollection"
            , criteria=' start_date_dt:[#fromDate# TO #toDate#} '
        );

// results 
writeDump( var=dateRangeResults, label="Date Range Search" );   


来源:https://stackoverflow.com/questions/57037249/how-to-pass-a-date-into-a-coldfusion-collection-to-be-searched

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!