3

Ok, so generally, when searching for a substring in a table, you can search for something similar using wildcards; i.e. `'%abc%' will return anything that contains that substring ('abc').

However, if I'm looking for a specific format (perhaps "abc123") can I use a regex match to search for the additional string data? Eg:

cmd.CommandText = "SELECT * FROM [tbl] WHERE [name] LIKE @Param;";

Regex r = "\d*";
cmd.Parameters.AddWithValue("@Param", "abc" + r);

This would then check all of your strings, and depending on the regex, it would compare the strings:

  • 'abc123' - Match
  • 'abcd123' - No match

Post note: I'm not really looking so much into the syntax of the regex right now, I'm more interested to know whether or not this kind of process is possible.

Ben
  • 2,433
  • 5
  • 39
  • 69
  • I think you can just use `=`, unless I am missing some nuance in your question. More importantly, why are you using SQL Server syntax on a question tagged "mysql" and asking about functionality not present in SQL Server? – Gordon Linoff Sep 29 '15 at 03:23
  • @Gordon this has nothing to do with SQL, this is specifically about MySQL. However, this is also very generalised in terms of syntax, even though SQL and MySQL *do* share similar syntax anyway. – Ben Sep 29 '15 at 03:26
  • Look for REGEXP LIKE - MySQL supports it. The regex flavor is POSIX regex. – nhahtdh Sep 29 '15 at 03:26
  • There is no standard SQL syntax for Regex, if a provider supports it, you will have to use provider specific syntax. – Ron Beyer Sep 29 '15 at 03:27
  • Have you checked this? - https://dev.mysql.com/doc/refman/5.5/en/regexp.html – Nikhil Vartak Sep 29 '15 at 03:32

1 Answers1

3

MySQL supports pattern matching operation based on regular expressions and the REGEXP operator.

SELECT name FROM person_tbl WHERE name REGEXP '^st';

You can have a look on the MySQL SELECT LIKE or REGEXP

Community
  • 1
  • 1
Mohit S
  • 13,723
  • 6
  • 34
  • 69