Select to view content in your preferred language

Where clause account for single quote (apostrophe)

254
2
Jump to solution
05-01-2025 10:25 AM
MK13
by
Frequent Contributor

Is there a way to standardise a where clause that may contain apostrophes (single quotes) in the string? I have a string 'RIVER'S EDGE CONDOMINIUM' that is causing the where clause in the query filter below to fail. How can I set up the where clause of the query filter to account for situations like the above while also accounting for regular string inputs? My code is shown below and it fails when the input string has an apostrophe in it. @CharlesMacleod @Wolf @UmaHarano 

var qf = new SpatialQueryFilter();
qf.FilterGeometry = myShape; 
qf.SpatialRelationship = SpatialRelationship.Intersects;
qf.WhereClause = $"{AllFieldnames.parcelNumFieldname} = '{intersctedParcelNum}'";

 

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

You have to replace the single quotation mark with two single quotation marks (the first one is used as an escape character).

var qf = new SpatialQueryFilter();
qf.FilterGeometry = myShape; 
qf.SpatialRelationship = SpatialRelationship.Intersects;
qf.WhereClause = $"{AllFieldnames.parcelNumFieldname} = '{intersctedParcelNum.Replace("'","''")}'";

 

View solution in original post

2 Replies
CharlesMacleod
Esri Regular Contributor

pre-process the string. i think "'" has to be escaped with another "'".

var str = "This has an embedded ' in it";
var strEscaped = str.Replace("'", "''");
qf.WhereClause = $"......'{strEscaped}'";
KenBuja
MVP Esteemed Contributor

You have to replace the single quotation mark with two single quotation marks (the first one is used as an escape character).

var qf = new SpatialQueryFilter();
qf.FilterGeometry = myShape; 
qf.SpatialRelationship = SpatialRelationship.Intersects;
qf.WhereClause = $"{AllFieldnames.parcelNumFieldname} = '{intersctedParcelNum.Replace("'","''")}'";