I am trying to filter fields in a table that contain percent (%) and apostrophe (') characters. I kow that to filter an apostrophe you need to add another apostrophe (''). However, when a text contains the percent (%) character in combination with an apostrophe (') character, an exception is thrown. Also, when using the % character in a filter string, it will be function as an "*" (fields starting or ending with). I need the % to be considered as a character whenever the % makes part of a string in the field. How can I address these issues?
The code below explains my question.
Dim TheTable As New DataTable
Dim col As New DataColumn("Col1")
TheTable.Columns.Add(col)
Dim TheRow As DataRow = TheTable.NewRow
TheRow("Col1") = "Filter me"
TheTable.Rows.Add(TheRow)
TheRow = TheTable.NewRow
TheRow("Col1") = "Filter me%"
TheTable.Rows.Add(TheRow)
TheRow = TheTable.NewRow
TheRow("Col1") = "Filter me'"
TheTable.Rows.Add(TheRow)
TheRow = TheTable.NewRow
TheRow("Col1") = "Filter me%'"
TheTable.Rows.Add(TheRow)
TheRow = TheTable.NewRow
Dim filteredrows() As DataRow
filteredrows = TheTable.Select("Col1 Like 'Filter me'")
MsgBox(filteredrows.Length)
'THE NEXT LINE WILL RETURN ALL RECORDS. I NEED ONLY THE RECORD ENDING WITH (OR CONTAINING) THE CHARACTER %
filteredrows = TheTable.Select("Col1 Like 'Filter me%'")
MsgBox(filteredrows.Length)
'THE NEXT LINE WILL RETURN TWO RECORDS (Filter me) and (Filter me'). I need it to return (Filter me') only
filteredrows = TheTable.Select("Col1 Like 'Filter me'''")
MsgBox(filteredrows.Length)
'THE NEXT LINE WILL THROW AND ERROR: "Error in Like operator: the string pattern 'Filter me%'' is invalid"
filteredrows = TheTable.Select("Col1 Like 'Filter me%'''")
MsgBox(filteredrows.Length)
The code below explains my question.
Dim TheTable As New DataTable
Dim col As New DataColumn("Col1")
TheTable.Columns.Add(col)
Dim TheRow As DataRow = TheTable.NewRow
TheRow("Col1") = "Filter me"
TheTable.Rows.Add(TheRow)
TheRow = TheTable.NewRow
TheRow("Col1") = "Filter me%"
TheTable.Rows.Add(TheRow)
TheRow = TheTable.NewRow
TheRow("Col1") = "Filter me'"
TheTable.Rows.Add(TheRow)
TheRow = TheTable.NewRow
TheRow("Col1") = "Filter me%'"
TheTable.Rows.Add(TheRow)
TheRow = TheTable.NewRow
Dim filteredrows() As DataRow
filteredrows = TheTable.Select("Col1 Like 'Filter me'")
MsgBox(filteredrows.Length)
'THE NEXT LINE WILL RETURN ALL RECORDS. I NEED ONLY THE RECORD ENDING WITH (OR CONTAINING) THE CHARACTER %
filteredrows = TheTable.Select("Col1 Like 'Filter me%'")
MsgBox(filteredrows.Length)
'THE NEXT LINE WILL RETURN TWO RECORDS (Filter me) and (Filter me'). I need it to return (Filter me') only
filteredrows = TheTable.Select("Col1 Like 'Filter me'''")
MsgBox(filteredrows.Length)
'THE NEXT LINE WILL THROW AND ERROR: "Error in Like operator: the string pattern 'Filter me%'' is invalid"
filteredrows = TheTable.Select("Col1 Like 'Filter me%'''")
MsgBox(filteredrows.Length)