Search code examples
jquerysqljsonsoql

Query with range returning empty


I'm building a simple query to recover some data based on the creation date of the record:

function buildQuery(startDate, endDate)
{
    var start_date = formattedDate(startDate);  //YYYY-MM-DD
    var end_date = formattedDate(endDate);      //YYYY-MM-DD
    var c_type = 'Noise';                                          // Complaint Type

    // Build the data URL
    URL = "http://data.cityofnewyork.us/resource/erm2-nwe9.json"; // API Access Endpoint
    URL += "?";                                                   // A query parameter name is preceded by the question mark
    URL += "$where=";                                             // Filters to be applied
    URL += "(latitude IS NOT NULL)";                              // Only return records with coordinates
    URL += " AND ";
    URL += "(complaint_type='" + c_type + "')";                    // Desired complaint
    URL += " AND ";
    URL += "(created_date>='" + start_date + "') AND (created_date<='" + end_date + "')"; // Date range
    URL += "&$group=complaint_type,descriptor,latitude,longitude";                        // Fields to group by
    URL += "&$select=descriptor,latitude,longitude,complaint_type";                       // Fields to return
    URL = encodeURI(URL);                                                                 // Encode special characters such as spaces and quotes
}

When I used startDate = endDate, no matter what date it is, the result .json, that I obtain using the following code, returns empty.

$.getJSON(URL, function(data)

However, if I check:

http://data.cityofnewyork.us/resource/erm2-nwe9.json

there is records that match the specific date. For example, this occurs when startDate and endDate are both equal to 2015-11-29.


Solution

  • @Rabbit appears to be right - the returned data contains "created_date" : "2015-11-30T02:14:24" indicating that the "date" has a time component. Change these two lines of your code:

    var start_date = formattedDate(startDate)+"T00:00:00";  //YYYY-MM-DDThh:mm:ss
    var end_date = formattedDate(endDate)+"T23:59:59";      //YYYY-MM-DDThh:mm:ss