1

I'm trying to construct a query.where statement using 2 input type=date fields on the page.

The date field in SQL Server in the format 2014-12-31 00:00:00.0000000. The input fields date is in the format 2014-12-31. I am getting errors using the following statement.

function execute () {
    query.geometry = addressPoint;
    startDateQuery = document.getElementById("startDate").value
    endDateQuery = document.getElementById("endDate").value
    query.where = "(DATE_VAL >= " + startDateQuery + ") AND (DATE_VAL <= " + endDateQuery + ")";
    queryTask.execute(query, showResults);
}

If I use the REST interface however, DATE_VAL >= '01-01-2014' AND DATE_VAL < '12-02-2014' works fine. So I am not sure if I need to use a formatting option for the query.where statemnent.

Date input fields:

        <div id="dateArea">
            Start Date: <input type="date" id="startDate" style="color:#333" /><br>
            End Date: &nbsp;<input type="date" id="endDate" style="color:#333" /><button id="SearchLightning" data-dojo-type="dijit/form/Button">Search</button>
        </div>
  • 1
    can you please confirm the exact values of startDateQuery and endDataQuery? What error message do you receive from the queryTask.execute? – Stephen Lead Jan 27 '15 at 21:45
  • From Dev toolserror: {code: 400, message: "Failed to execute query.", details: []} code: 400 details: [] message: "Failed to execute query." – Andrew Jan 27 '15 at 21:51
  • The exact values will be from the date input fields. I have added the code. When I used console.log(startDateQuery) is 2014-01-01 and console.log(endDataQuery) is 2014-12-31. – Andrew Jan 27 '15 at 21:54
  • 1
    It looks like you need to transpose the date format from 2014-01-01 to 01-01-2014 (I'm not sure the best way to do this). You should also enclose the dates within single quotes: query.where = "(DATE_VAL >= '" + startDateQuery + "') AND (DATE_VAL <= '" + endDateQuery + "')"; – Stephen Lead Jan 27 '15 at 22:39
  • @StephenLead Actually your code worked. I did not have to modify the date format at all. Thank you! – Andrew Jan 28 '15 at 14:09
0

Note that your REST query encloses the dates within single quotes:

DATE_VAL >= '01-01-2014'

To match this exactly, you need to enclose the dates within single quotes:

query.where = "(DATE_VAL >= '" + startDateQuery + "') AND (DATE_VAL <= '" + endDateQuery + "')";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.