query Date Range syntax

2397
9
Jump to solution
04-03-2017 12:04 PM
jaykapalczynski
Frequent Contributor

Wondering if anyone can help with the syntax to query a date range?

query.where = "County_Nam = '" + (dom.byId("CountyName").value) + "' AND Squad = '" + (dom.byId("Squad").value) + "' ";

query.where = "County_Nam = '" + (dom.byId("CountyName").value) + "' AND Squad = '" + (dom.byId("Squad").value) + "' AND DateIncident = '" + (dom.byId("StartDate").value) + "' BETWEEN DateIncident = '" + (dom.byId("EndDate").value) + "' ";

0 Kudos
2 Solutions

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

And here is how I do most of mine:

query.where = "DateIncident BETWEEN timestamp '" + dom.byId("StartDate").value + "' AND timestamp '" + dom.byId("EndDate").value + "'"‍‍‍‍

View solution in original post

RobertScheitlin__GISP
MVP Emeritus

@RobertKirkwood GETDATE() is a SQL server function if you want the date from JS code then it would look like this:

 

query.where = "'" + new Date().toJSON().slice(0,10).replace(/-/g,'/') + "' >= EXPIRATION_DATE";

 

The current date will look like "2022/06/15" based on the JS code above

View solution in original post

9 Replies
RobertScheitlin__GISP
MVP Emeritus

Jay,

  What is the underlying data database type (SQL Server, Shapefile, FGDB, Oracle, AGOL)?

0 Kudos
jaykapalczynski
Frequent Contributor

SQL Server...This query works fine....just need the Date Range

0 Kudos
Drew
by
Occasional Contributor III

We use MS SQL Server and we query our dates like so..

SAMPLE

query.where = "CreatedDateTime >= '2017/03/27 00:00:00 AM' AND CreatedDateTime <= '2017/04/03 11:59:59 PM'"

Sometimes its easier to test these things on the REST Endpoint too.. 

Drew

jaykapalczynski
Frequent Contributor

Thank Andrew....your solution worked fine...

0 Kudos
RobertKirkwood
Occasional Contributor III

How would i query value less than todays date? i was trying something like 

query.where = GETDATE() >= "EXPIRATION_DATE"

But this is not working. Any thoughts?  

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

@RobertKirkwood GETDATE() is a SQL server function if you want the date from JS code then it would look like this:

 

query.where = "'" + new Date().toJSON().slice(0,10).replace(/-/g,'/') + "' >= EXPIRATION_DATE";

 

The current date will look like "2022/06/15" based on the JS code above

RobertKirkwood
Occasional Contributor III

that worked perfectly! Thank you!

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

And here is how I do most of mine:

query.where = "DateIncident BETWEEN timestamp '" + dom.byId("StartDate").value + "' AND timestamp '" + dom.byId("EndDate").value + "'"‍‍‍‍
jaykapalczynski
Frequent Contributor

Had to add two )  after value for start and end date but worked great

(dom.byId("StartDate").value

(dom.byId("StartDate").value)
0 Kudos