Filtering special characters in SQL Server - sql-server-2014

This is question pertaining to SQL Server 2014. I have a table xxx. There is a column col1 of type varchar. The values in this column can have alphanumeric characters like 1A324G. There can also be special characters along with alphanumeric like !2A93C or #AC934D, etc.
There can be any special character (eg: !$#^().-_) in a value for this column. I wanted to extract data with only alphanumeric values and NOT any special characters in it. I was trying to use the LIKE clause with wildcard search pattern but I am not able to weed out the ones with only alphanumeric values.
Can someone please help me and let me know how I can do it?

It's been a while since I've played with sql but something like this should work.
SELECT *
FROM xxx
WHERE col1 NOT LIKE '%!%' OR '%$%';

Related

mysql MATCH AGAINST weird characters query

I have a table where the field "company_name" has weird characters, like "à","ö","¬","©","¬","†", etc. I want to return all "company_name"s that contain these characters anywhere within the string. My current query looks like this:
SELECT * FROM table WHERE
MATCH (company_name) AGAINST ('"Ä","à","ö","¬","©","¬","†"' in natural language mode);
But I keep getting no data from the query. I know this can't be the case, as there are definitely examples of them I can find manually. To be clear, the query itself isn't throwing any errors, just not returning any data.
The minimun word length is 3 pr 4 .
you can change it see manial
https://dev.mysql.com/doc/refman/8.0/en/fulltext-fine-tuning.html
or use regular expressiions
SELECT * FROM table WHERE
ompany_name REGEXP '[Äàö¬©¬†]+';
SELECT *
FROM table
WHERE company_name LIKE '%[^0-9a-zA-Z !"#$%&''()*+,\-./:;<=>?#\[\^_`{|}~\]\\]%' ESCAPE '\'
This will find any wacky stuff, including wide characters or 'euro-ASCII' or emoji.

SQL conditional: using Regex formatter for "Like"

I have a record in a database like this: 1K-05, in a column called "DocXmtlNum"
The SQL statement to try to get it is like this:
"SELECT DISTINCT DocXmtlNum FROM table1 WHERE DocXmtlNum Like '#?[A-Z]*' ORDER BY DocXmtlNum Desc"
However, it does not grab any records. I am assuming that the "#?[A-Z]*" part is saying that it wants to get records that start with a number, is followed by a letters, and is followed by any other characters. What's wrong with this? How would I write the regular expression to get a record that is a number followed by a letter, and followed by any character?
Note: The SQL statement was auto translated from VB6 to vb.net4, so there were errors introduced.
Is this what you want?
WHERE DocXmtlNum REGEXP '^[0-9]?[A-Z]-.+$'
This checks for:
An optional digit
A letter
A hyphen
At least one more character

Search of String value having special character in MySQL

I have one table emp in MySQL database having column as name. In that name column, the value is 'abc\xyz'. I want to search this value. I have tried using following query:
select * from emp where name like 'abc\xyz';
Also i have tried
select * from emp where name like 'abc\xyz' escape '\\';
But i did not found any output. Could you please help me in finding such strings? Such strings can have special character at any location.
Thanks in advance.
You may try like this:
select * from emp
where empname like '%abc\\\\xyz%'
SQL Fiddle Demo
From the docs:
Because MySQL uses C escape syntax in strings (for example, “\n” to represent a newline character), you must double any “\” that you use in LIKE strings. For example, to search for “\n”, specify it as “\\n”. To search for “\”, specify it as “\\\\”; this is because the backslashes are stripped once by the parser and again when the pattern match is made, leaving a single backslash to be matched against.
SELECT REPLACE(text,'\\','') FROM tbl
You can use REPLACE to remove some special chars :)

How to remove fake names using regular expression in mysql query?

I want to remove the names which may be registered with fake names.
As the developer forgot to put validation on form registration.
Now i want to remove the fake names.
And for checking if that name is fake or not, I am checking if the name content any numbers or not ?
This is my query which i have written but its not working...
SELECT registration.regi_id, student.first_name,
student.cont_no, student.email_id,
registration.college,
registration.event_name,
registration.accomodation
FROM student, registration
WHERE student.stud_id = registration.stud_id
AND student.first_name NOT RLIKE '%[0-9]%'
How to fix this problem ?
Sorry for my language issues,
P.S.
There are many names in "first_name" field like "asdfasdf12323", i don't want that kind of names to be shown on list.
Your column may contain Alphanumeric characters also.YOu need to filter Numbers and Alphanumeric characters both
For Alphanumeric characters Try REGEXP '^[A-Za-z0-9]+$'
For numbers Try REGEXP '[0-9]'
Well as far as the regex is involved, your expression is only looking for a single number. Also, your 'NOT RLIKE' isn't using regex but is doing a basic string search for the literal '[0-9]' I believe. MySql has support for regex, and your last clause would look like so: AND student.first_name NOT REGEXP '[0-9]*'

MYSQL SELECT LIKE "masks"

I'm using a field in a table to hold information about varios checkboxes (60).
The field is parsed to a string to something like this
"0,0,0,1,0,1,0,1,..."
Now I want to make a search using a similar string to match the fields. I.e.
"?,?,1,?,?,1,..."
where the "?" means that it must be 0 or 1 (doesn't matter), but the "1" must match.
As i've seen the '%' is somewhat innapropriate for this case, don't?
Obviosly both strings have the same lenght.
Suggestions?
You can use the underscore (_) character to match a single character in the mask.
Taken from MySQL documentation.