SQL Selecting Values not working - mysql

I have a chemistry database (very small, about 60kb or so) in a mysql database. My
select * from firstdatabase;
works fine with any column but symbol. When I do
select * from firstdatabase where symbol = "Y";
for example, I get an empty set. However, when I do
select symbol from firstdatabase;
I get every symbol in the database including "Y". This problem doesn't occur with any other field except symbol. I have also tried it with double lettered and triple lettered elements to no avail. Please help?

My first guess is your symbol column contains whitespace characters. So you should try TRIM function.
select * from firstdatabase where TRIM(symbol) = "Y"

Use LTRIM and RTRIM if there whitespace characters in your symbol column.You should try LTRIM and RTRIM function.
select * from firstdatabase where LTRIM(RTRIM(symbol))= 'Y'

Related

Why isn't MySQL REGEXP filtering out these values?

So I'm trying to find what "special characters" have been used in my customer names. I'm going through updating this query to find them all one-by-one, but it's still showing all customers with a - despite me trying to exlude that in the query.
Here's the query I'm using:
SELECT * FROM customer WHERE name REGEXP "[^\da-zA-Z\ \.\&\-\(\)\,]+";
This customer (and many others with a dash) are still showing in the query results:
Test-able Software Ltd
What am I missing? Based on that regexp, shouldn't that one be excluded from the query results?
Testing it on https://regex101.com/r/AMOwaj/1 shows there is no match.
Edit - So I want to FIND any which have characters other than the ones in the regex character set. Not exclude any which do have these characters.
Your code checks if the string contains any character that does not belong to the character class, while you want to ensure that none does belong to it.
You can use ^ and $ to check the while string at once:
SELECT * FROM customer WHERE name REGEXP '^[^\da-zA-Z .&\-(),]+$';
This would probably be simpler expressed with NOT, and without negating the character class:
SELECT * FROM customer WHERE name NOT REGEXP '[\da-zA-Z .&\-(),]';
Note that you don't need to escape all the characters within the character class, except probably for -.
Use [0-9] or [[:digit:]] to match digits irrespective of MySQL version.
Use the hyphen where it can't make part of a range construction.
Fix the expression as
SELECT * FROM customer WHERE name REGEXP "[^0-9a-zA-Z .&(),-]+";
If the entire text should match this pattern, enclose with ^ / $:
SELECT * FROM customer WHERE name REGEXP "^[^0-9a-zA-Z .&(),-]+$";
- implies a range except if it is first. (Well, after the "not" (^).)
So use
"[^-0-9a-zA-Z .&(),]"
I removed the + at the end because you don't really care how many; this way it will stop after finding one.

MySql regexp escaping apostrophe(’)

I can't find a proper way to escape apostrophe sign(’) in my mysql query. Regexp I have, works fine with online tools for regexp testing.
Problematic example is the string G’Schlössl.
I want to have optional apostrophe sign in the query in front of the s character G(’?)Schlö(’?)ssl for all the different cases which could occur in other strings. I am not sure if the problem is caused by incorrect sign escaping but I have tried many options like ’?, \’?, \’{0,1} which works for the first occurrence but doesn't for the second optional one and cause query to return nothing. Other possibilities like ’’?, [’]?, [\’]?, [\’]{0,1} does not work even for the first one.
select id, name from restaurant where name regexp '.*g\’?(s|ß|ss|sz)chl(o|ö|oe)\’?s.*';
When I remove the last \’? it works:
select id, name from restaurant where name regexp '.*g\’?(s|ß|ss|sz)chl(o|ö|oe)s.*';
When I replace the last \’? with x? it works as well:
select id, name from restaurant where name regexp '.*g\’?(s|ß|ss|sz)chl(o|ö|oe)x?s.*';
Any ideas where the problem is or what else to try?
This thread explains escaping normal single quote only, which seems not to work in my case.
Instead of \’?, try (’)?. I'm thinking that the ? may apply to only the last byte of ’. By using parentheses instead, the ? applies to the entire 3 bytes (hex E28099) of the "RIGHT SINGLE QUOTATION MARK".

matching escape charactres using like operator in mysql

I want to match the string having escape characters with particular column in a table.
SELECT * FROM table WHERE col LIKE 'MESSRESTAURANGER AB\\MESSVEGEN 1\\STOCKH';
Though there is matching data in table, query result is empty set. But the same query works fine in oracle. What is the issue with mysql?
You miss %:
SELECT * FROM table WHERE col LIKE '%MESSRESTAURANGER AB\\MESSVEGEN 1\\STOCKH%';
But it should work without escaping:
SELECT * FROM table WHERE col LIKE '%MESSRESTAURANGER AB\MESSVEGEN 1\STOCKH%';
Fiddle http://sqlfiddle.com/#!9/a7ba59/2
EDIT:
SELECT * FROM t WHERE n LIKE '%MESSRESTAURANGER AB\\\\\\\\MESSVEGEN 1\\\\\\\\STOCKH%'
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.
Fiddle http://sqlfiddle.com/#!9/ac46b/9

Having trouble matching a single character in an SQL table

I need to use the '_' wildcard to find all id that are only one letter which there are a few of. However when I run my query no rows are returned.
Heres my query:
SELECT *
FROM table
WHERE id LIKE '_';
I have a table lets call Table1 that has two columns, id and name.
id either has 1 or 2 characters to label a name. I'm trying to only find the names where the id is only one character. Heres an example of the table:
id name
A Alfred
AD Andy
B Bob
BC Bridget
I only want to return Alfred and Bob in this example.
I don't want the solution but any advice or ideas would be helpful.
Here is a screenshot of my query:
http://i.imgur.com/EWTfoVI.png?1
And here is a small example of my table:
http://i.imgur.com/urGRZeK.png?1
So in this example of my table I would ideally like only East Asia... to be returned.
I if I search specifically for the character it works but for some strange reason the '_' wildcard doesn't.
For example:
SELECT *
FROM icao
WHERE prefix_code ='Z';
This works.
Try using TRIM
Select *
FROM [Table]
where TRIM(ID) LIKE '_';
In MySQL, the underscore is used to represent a wildcard for a single character. You can read more about that Pattern Matching here.
The way you have it written, your query will pull any rows where the id column is just one single character, you don't need to change anything.
Here is an SQL Fiddle example.
EDIT
One trouble shooting tip is to be sure there is no whitespace before/after the prefix code. If there is, and you need to remove it, add TRIM():
SELECT *
FROM myTable
WHERE TRIM(id) LIKE '_';
Here is an example with TRIM.
EDIT 2
A little explanation to your weird behavior, hopefully. In MySQL, if there is trailing white space on a character, it will still match if you say id = 'Z'; as seen by this fiddle now. However, leading white space will not match this, but will still be corrected by TRIM(), because that removes white space on the front and back end of the varchar.
TL;DR You have trailing white space after Z and that's causing the problem.
The most likely explanation for the behavior you observe is trailing spaces (or other whitespace) in the value. That is, you see one character
'A'
But the value may actually be stored as two (or more) characters.
'A '
To see what's actually stored, you can use the HEX and LENGTH functions.
SELECT t.foo
, LENGTH(t.foo)
, HEX(t.foo)
FROM mytable t
WHERE t.foo LIKE 'A%'
The % is a wildcard for the LIKE operator that matches any number of characters (zero, one or more).
You can use the RTRIM() function to remove trailing spaces...
SELECT RTRIM(t.foo)
, LENGTH(RTRIM(t.foo))
, HEX(RTRIM(t.foo))
FROM mytable t
WHERE t.foo LIKE 'A%'
SELECT *
FROM table
WHERE LENGTH(id)=1
Strange..., in my case works perfectly (I am using mysql 5.5).
Please, try this:
select * from mysql.help_topic where name like '_';
What set you get?

Strip last two characters of a column in MySQL

I have an SQL column where the entries are strings. I need to display those entries after trimming the last two characters, e.g. if the entry is 199902345 it should output 1999023.
I tried looking into TRIM but looks like it offers to trim only if we know what are the last two characters. But in my case, I don't know what those last two numbers are and they just need to be discarded.
So, in short, what MySQL string operation enables to trim the last two characters of a string?
I must add that the length of the string is not fixed. It could be 9 characters, 11 characters or whatsoever.
To select all characters except the last n from a string (or put another way, remove last n characters from a string); use the SUBSTRING and CHAR_LENGTH functions together:
SELECT col
, /* ANSI Syntax */ SUBSTRING(col FROM 1 FOR CHAR_LENGTH(col) - 2) AS col_trimmed
, /* MySQL Syntax */ SUBSTRING(col, 1, CHAR_LENGTH(col) - 2) AS col_trimmed
FROM tbl
To remove a specific substring from the end of string, use the TRIM function:
SELECT col
, TRIM(TRAILING '.php' FROM col)
-- index.php becomes index
-- index.php.php becomes index (!)
-- index.txt remains index.txt
Why not using LEFT(string, length) function instead of substring.
LEFT(col,char_length(col)-2)
you can visit here https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_left to know more about Mysql String Functions.
substring().
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html
You can use a LENGTH(that_string) minus the number of characters you want to remove in the SUBSTRING() select perhaps or use the TRIM() function.