Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if a string contains special characters?

Tags:

sql

sql-server

How to detect if a string contains special characters like #,$,^,&,*,@,! etc (non-alphanumeric characters) in SQL server 2005?

like image 701
Manish Avatar asked Apr 01 '10 07:04

Manish


People also ask

How do you check if a string contains any special character in C#?

new Regex(“[^A-Za-z0-9]”) the pattern is used to check whether the string contains special characters or not. IsMatch() function checks the string and returns True if the string contains a special character. int. TryParse(str, out int n) the method returns True if the string contains the number(s).


1 Answers

Assuming SQL Server:

e.g. if you class special characters as anything NOT alphanumeric:

DECLARE @MyString VARCHAR(100) SET @MyString = 'adgkjb$'  IF (@MyString LIKE '%[^a-zA-Z0-9]%')     PRINT 'Contains "special" characters' ELSE     PRINT 'Does not contain "special" characters' 

Just add to other characters you don't class as special, inside the square brackets

like image 148
AdaTheDev Avatar answered Sep 22 '22 06:09

AdaTheDev