26

I have the following REGEX: ^[-A-Za-z0-9/.]+$

This currently checks whether the value entered into a textbox matches this. If not, it throws an error.

I need to check whether anything has already gone into the database that doesnt match this.

I have tired:

 SELECT * FROM *table* WHERE ([url] NOT LIKE '^[-A-Za-z0-9/.]+$') 
 SELECT * FROM *table* WHERE PATINDEX ('^[-A-Za-z0-9/.]+$', [url])

UPDATE

So after a bit of research I've realised I don't think I can use REGEXP.

I thought I could do something like this? Its not giving me the expected results but its running unlike anything else. Can anyone spot anything wrong with it?

SELECT *, 
  CASE WHEN [url] LIKE '^[-A-Za-z0-9/.]+$' 
    THEN 'Match' 
    ELSE 'No Match' 
  END Validates
FROM 
  *table*
eggyal
  • 122,705
  • 18
  • 212
  • 237
Clare Barrington
  • 1,135
  • 1
  • 11
  • 28
  • 2
    Are you using MSSQL or MySQL? They are two, different, competing RDBMS products from different suppliers and which have different syntax. – eggyal Mar 23 '15 at 10:57

3 Answers3

29

This is what I have used in the end:

SELECT *, 
  CASE WHEN [url] NOT LIKE '%[^-A-Za-z0-9/.+$]%' 
    THEN 'Valid' 
    ELSE 'No valid' 
  END [Validate]
FROM 
  *table*
  ORDER BY [Validate]
Braiam
  • 1
  • 11
  • 47
  • 78
Clare Barrington
  • 1,135
  • 1
  • 11
  • 28
  • 10
    I am very sure the answer does not work as intended, because MS-SQL supports only a very limited subset of RegExp like operators: https://msdn.microsoft.com/en-us/library/ms187489(v=sql.105).aspx, hence the expression provided in [] matches one single character from the set ^-A-Za-z0-9/.+$ and the rest is consumed by % which will match any string in LIKE expression. – Mr. Napik May 17 '16 at 12:09
  • 1
    @Mr.Napik - The idea is sound though it is collation dependent whether or not it actually works as written. `^` acts as a negation. So instead of saying "check every character matches the pattern" it checks that there are no characters that don't match the pattern. It probably needs a binary collate clause to actually work as intended though. e.g. see `SELECT char(number) FROM master..spt_values WHERE type='P' and number between 1 and 255 and CHAR(number) NOT LIKE '[^-A-Za-z0-9/.+$]' COLLATE Latin1_General_Bin` with and without it. – Martin Smith Oct 23 '16 at 19:04
19

Disclaimer: The original question was about MySQL. The SQL Server answer is below.

MySQL

In MySQL, the regex syntax is the following:

SELECT * FROM YourTable WHERE (`url` NOT REGEXP '^[-A-Za-z0-9/.]+$') 

Use the REGEXP clause instead of LIKE. The latter is for pattern matching using % and _ wildcards.


SQL Server

Since you made a typo, and you're using SQL Server (not MySQL), you'll have to create a user-defined CLR function to expose regex functionality.

Take a look at this article for more details.

Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
-3

As above the question was originally about MySQL

Use REGEXP, not LIKE:

SELECT * FROM `table` WHERE ([url] NOT REGEXP '^[-A-Za-z0-9/.]+$')
Matt
  • 14,906
  • 27
  • 99
  • 149
  • 1
    @Azimuth Regular expression information on SQL Server can be found here https://learn.microsoft.com/en-us/sql/relational-databases/scripting/search-text-with-regular-expressions – Matt Feb 05 '18 at 14:12
  • 1
    @Matt The link you provided refers to __editing__ the query, rather than valid syntax of SQL Server. More important thing is that even in SQL Server 2017 running similar query returns `An expression of non-boolean type specified in a context where a condition is expected, near 'REGEXP'.` error - I assume there is no such keyword yet. – bjauy Feb 08 '18 at 12:04
  • Yes, as @bjauy pointed, there is no such keyword as `REGEXP`. So please update your answer with correct SQL code. – Azimuth Feb 15 '18 at 08:24