How can I return Special Characters in SQL? - sql

I've been searching and I couldnt found exactly what I need.
I have and SQL Server 2008
I have some strings in a table with special characters such as "!,;:-()" and I'm trying to make a script that could return this characters, BUT ONLY THOSE CHARACTERS (do you get it?)
For example, I have "Borges, Ricardo" and I need to return only the ","
Another example, "Calle 13 Nº 34, Mercedes Bs As ( 6600)", and I only need the "º,()"
I dont want to get the letters or numbers.
I wrote this simple script:
SELECT name FROM table WHERE name LIKE '%[^A-Za-z0-9 ]%'
SO I could get all the rows where there is a Special Character like a (,;.:-()&%$... but I need to RETURN ONLY the special Characters. Get it? No letters, no numbers, only the special characters in the row.
THank you very much for your help!

Related

Getting the Column containing the non-english language in ORACLE

I have above entries in my database, my requirement is to extract the fields containing the non-english language characters ( including if the data containing the combination of english and non-english characters like HotelName field for the ID 45).
I tried by regexp_like function by looking for the alphanumeric and non-alphanumeric, but i have some data with combination of both the condition fails there.
Thanks in Advance
Raghavan
Does this do what you want?
where regexp_like(hotelname, '[^a-zA-Z0-9 ]')
That is, where the hotel name contains any character that is not a "letter" or digit. You may need to take additional characters into account as well, such as commas, periods, and hyphens.

SQL Text Function for special characters

I have a field with text reviews in it and I want to spot where people have used special characters to get offensive words etc past the filters, so instead of typing badword they type b.a.d.w.o.r.d or b*a*d*w*o*r*d,
Is there a way to look for say 3 or more special characters in word in a text review, maybe some sort of count function for special characters?
If you have a table with a field containing words you dont want to allow you could add it in your WHERE clause like so using REGEX_REPLACE.
SELECT yourfield
FROM yourtable
WHERE REGEXP_REPLACE(yourfield,'[^a-zA-Z'']','') NOT IN (SELECT badwords
FROM badwordstable)

SQL find all rows that do not have certain characters

I want to find all rows for which values in string column does not possess certain characters (to be specific [a-Z0-9 \t\n]) how can I do it in sql ?
I tried to do it with like operator
SELECT ***
where column like '%[^ a-Z0-9 \t\n]%'
however, it does not work and I get rows that possess characters and numbers.
To fetch all records that contain any characters other than alphabets, numbers, spaces, tabs and new-line delimiters:
SELECT ***
WHERE column like '%[^A-Za-z0-9 \t\n]%'
Note that [^A-Za-z0-9 \t\n] represents anything other than alphanumeric characters, spaces, tabs, and new line delimitters.
Your logic is inverted. I think you want:
where column not like '%[^ a-Z0-9 \t\n]%'
I don't think that SQL Server interprets \t and \n as special characters. You may need to insert the actual values for the characters. (See here.)
SELECT ***
WHERE column like '%[^A-Za-z0-9 \t\n]%'

Comma inside like query fails to return any result

Using Oracle db,
Select name from name_table where name like 'abc%';
returns one row with value "abc, cd" but when I do a select query with a comma before % in my like query, it fails to return any value.
Select name from name_table where name like 'abc,%';
returns no row. How can I handle a comma before % in the like query?
Example:
Database has "Sam, Smith" in the name column when the like has "Sam%" it returns one row, when i do "Sam,%" it doesn't return any row
NOT AN ANSWER but posting it as one since I can't format in a comment.
Look at this and use DUMP() on your own machine... see if this helps.
SQL> select dump('Smith, Stan') from dual;
DUMP('SMITH,STAN')
-----------------------------------------------------
Typ=96 Len=11: 83,109,105,116,104,44,32,83,116,97,110
If you count, the string is 11 characters (including the comma and the space). The comma is character 44, and the space is character 32. If you look at YOUR string and you don't see 44 where the comma should be, you will know that's the problem. You could then let us know what you see there (just for that character, I understand posting "Leno, Jay" would be a violation of privacy).
Also, make sure you don't have any extra characters (perhaps non-printable ones!) right before the comma. Just compare the two strings you are using as inputs and see where the differences may be.

SQLite regular Expressions regex get exact word by number

I have a string like the following in a sqlite 1 column:
1a 2B 3c 354 AfS 151 31s2fef 1fs31 3F1e2s 84f64e 45fs
space separated, x amount of characters 0-9, a-z, A-Z, there might be punctuation I'm not sure, but it is definitely space separated.
I'm trying to make a regular expression so I can query the database by number of words. basically if I wanted to get the 6th "word" in the example I'd be looking for:
151
so I tried to make a regular expression that says if the Nth word = 151, return me that row.
Here's what I've got so far.
SELECT * FROM table1 WHERE column1 REGEXP ^((?:\S+\s+){1}){6}
That unfortunately gives me the first through sixth words, but I really want to pinpoint the 6th word like the example above.
Also, I was thinking so save room in the database I could get rid of the white space, I'd just need to know how to count a specific number of characters in to the string which I couldn't figure out either.
Thanks for the help, never written a regular expression before.
If all you need is a match, there is no need for the non-capturing group. Just match a non-space + space group 5 times and follow with a 151.
^(\S+\s+){5}151
Use following regex
^([^\s]+\s){6}(.*?)(\s|$)
this must return 151, you can change {6} with your number to match the string.