SQL syntax for query.where.

895
7
Jump to solution
08-13-2013 01:01 PM
ZachLiu1
Occasional Contributor II
I am trying to build a query form on my application, but I met problems building SQL statements.
Anyone knows where to find accurate SQL syntax for query.where?

e.g. This statement does not work.

query.where = "damname = '" + $("input[name='damname']").val() + "'" + " OR " + "HAZCLASS = " + $("input[name='HAZCLASS']").val() + "";
0 Kudos
1 Solution

Accepted Solutions
JasonZou
Occasional Contributor III
Try:
var damValue = $.trim($("input[name='damname']").val()); var hazValue = $.trim($("input[name='HAZCLASS']").val());  var damExpr = "damname = '" + damValue + "'"; var hazExpr = hazValue ? "HAZCLASS = " + hazValue : ""; query.where = damExpr + hazExpr ? " OR " + hazExpr : "";

View solution in original post

0 Kudos
7 Replies
DerekMaune
New Contributor
You don't have quotes around your second value.  It sounds like you're having more than one problem with the syntax though.  Can you provide more detail?  Is it throwing an error or just returning an empty dataset?
0 Kudos
ZachLiu1
Occasional Contributor II
The second value is a number so i didn't quote it. Both statements work fine, but I can't join them with a "OR".
0 Kudos
VinayBansal
Occasional Contributor II
There is no need to add string in query at last. This should work

query.where = "damname = '" + $("input[name='damname']").val() + "'" + " OR " + "HAZCLASS = " + $("input[name='HAZCLASS']").val();


Also, get the values that are coming during debugging in query.where and put that values in your ArcGIS Rest interface to test if it works there.
0 Kudos
ZachLiu1
Occasional Contributor II
If I do this and the HAZCLASS input is empty, it will report query failure.

I am using this so far. Don't know if there's a simple way.

query.where = "damname = '" + $("input[name='damname']").val() + "'" + " or " + "HAZCLASS=" + ($("input[name='HAZCLASS']").val()? $("input[name='HAZCLASS']").val(): "'null'");
0 Kudos
JanJeske
New Contributor III
Hey try this if u need to query for NULL val try this:
In the where clause its simple sql 😉
var val1= $("input[name='damname']").val();
var val2= $("input[name='HAZCLASS']").val();
query.where = "damname = '" + val1+ "' or HAZCLASS " + ((val2 != "")?" = "+val2:" IS NULL");


if there is no need try this:
var val1= $("input[name='damname']").val();
var val2= $("input[name='HAZCLASS']").val();
query.where = "damname = '" + val1+ "' " + ((val2 != "")?"HAZCLASS  = "+val2:"");
0 Kudos
JasonZou
Occasional Contributor III
Try:
var damValue = $.trim($("input[name='damname']").val()); var hazValue = $.trim($("input[name='HAZCLASS']").val());  var damExpr = "damname = '" + damValue + "'"; var hazExpr = hazValue ? "HAZCLASS = " + hazValue : ""; query.where = damExpr + hazExpr ? " OR " + hazExpr : "";
0 Kudos
ZachLiu1
Occasional Contributor II
Thanks Jason, I made a little change of your code and it works perfect for the purpose.

query.where = damExpr + (hazExpr ? " OR " + hazExpr : "");


Also, thanks Jan:)
0 Kudos