I want to delete a record which contains %
sign, for example:
white%House, Den%mark, bengali%20Tiger, White%202.
I have this query but it doesn't work for me.
delete from nail where name like '%[%]%'
I am using C# and Access.
I want to delete a record which contains %
sign, for example:
white%House, Den%mark, bengali%20Tiger, White%202.
I have this query but it doesn't work for me.
delete from nail where name like '%[%]%'
I am using C# and Access.
I think there is some confusion going on, so here is a complete code example that works for me:
using ( var conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\users\john\desktop\Database11.accdb;Persist Security Info=False;"))
{
conn.Open();
OleDbCommand cmd =
new OleDbCommand("DELETE FROM TABLE1 where [name] like '%[%]%'", conn);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
}
Note that you are using "name" as one of your fields, which is a reserved word, so you need to escape it inside of brackets. The same goes for the %, since it is the wildcard character that is used by OLEDb.
You can use InStr()
to find if the name contains %
where InStr(name, '%') > 0
For completeness sake:
Within MS Access running in its default SQL mode (ANSI 89), you would use:
Like "*%*"
Likewise, if you're using DAO to execute the SQL, you'd use that.
You only run into problems here when running in ANSI 92 mode, which is available within Access only if you have ANSI 92 turned on, or if you're using ADO, or if you use ALIKE
instead of LIKE
. In that case, you have to use the more standard %
wildcard. If you're running within Access or using DAO, you can use ALIKE
to allow you to use the %
wildcard:
ALike "%[%]%"
But if you're running Access in SQL 92 mode, you don't need ALIKE
at all, you can just use LIKE
(but I would never recommend running Access in SQL 92 mode, as it breaks a number of things within Access).
If you're using ADO/OLEDB to execute the SQL, your only option is SQL 92 mode, and you should be able to use either ALike "%[%]%"
or Like "%[%]%"
. I don't know whether the double quotes make a difference or not, though. In my tests using ADO within Access, it didn't make any difference.
The following worked for me native in MS Access
DELETE foo.foo1
FROM foo
WHERE (((foo.foo1) ALike "%[%]%"));
Maybe it's the double quotes instead of the apostrophes.
Perhaps in C# you need to do something like
bar = "DELETE foo.foo1 FROM foo WHERE (((foo.foo1) ALike \"%[%]%\"));";