i have one table which consists of 10 columns out of which one column is username . the column username stores the name of student which may be in uppercase and lowercase .
i want to segregate the uppercase and lowercase students.if the username consists of any uppercase it will list the row.
i am interested in doing query for column username.in other column also uppercase letters are there but i want to list based on username column only. i have tried several query but no one is working.please advice
i want to list rows with any upperletter in column username.
i have tried these codes
SELECT * FROM accounts WHERE LOWER(username) LIKE '%q'
did not worked
SELECT * FROM accounts WHERE UPPER(username) = UPPER('%q')
did not worked
SELECT * FROM accounts where username COLLATE latin1_swedish_ci = '%q'
did not worked
SELECT * FROM accounts WHERE username REGEXP '[A-Z]';
did not worked
SELECT * FROM accounts WHERE username REGEXP '^[[:upper:]+]$'
did not worked
SELECT *
FROM accounts
WHERE CAST(username AS BINARY) RLIKE '[A-Z]';
CREATE TABLE accounts (
id int,
username varchar(50)
) CHARACTER SET latin1 COLLATE latin1_general_ci;
SELECT* FROM accounts WHERE username REGEXP '^[A-Z]+$';
Make sure you use COLLATE latin1_general_ci
You were on track with the collation, but you need to have a table that is collated, not just the query. What you could do is create a new table, then insert your current rows into the new collated table, then try the REGEX or the rest of the methods.
Select ALL fields that contains only UPPERCASE letters
The following query will work fine
select * from TABLE where CAST( COL_NAME AS BINARY) = upper(COL_NAME);
First, you need to make sure the field you are searching on have a case-specific collation like latin1_general_cs (if you are using latin character set). Then you can just search for uppercase or lower case, whichever is you are looking for (i.e. WHERE username LIKE '%q%' or WHERE username LIKE '%Q%'
Mysql is case insensitive for strings, so it will be more complecated than a single seletect statement. If you want to do this comparison often, convert the type of the username column to one of the binary types listed below:
http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html
If you don't want to do this often, consider saving off the results of the current table to a temp table, altering that table with a case sensitive string type, and then using your regex.
Related
Is there a way to match values in query if the stored data has special characters, and the search query doesn't:
For example: I want to match a column with the following value:
Doña Ana
but I can only search using
Dona Ana
You may collate the column of interest to Latin general, which doesn't have accents:
SELECT *
FROM yourTable
WHERE name COLLATE latin1_general_ci = 'Dona Ana';
I am trying to insert emojis into my SQL table. I have some emojis already inserted in my rows from my table, like these one: ⚽(\u26bd), ⭐(\u2b50) and ⛔ (\u26d4).
I have already tried to insert and update my rows from my table with different emojis, but i can't. I have discovered the reason that MySQL has allow me to insert the other 3 emojis and it's because the codification of they only have one \. If i try to insert a different emoji which the ascii codification has more than \, the information in the row will magically disappear, or it will be replaced with ????.
This is my query to update the one row from my table to add an emoji:
UPDATE myusers SET name = "\u26bd William" WHERE id = 8
If i run the query in my table it will insert in the row name \u26bd William, as i want.
But if i try with a different emoji which ascii codification it's like this 🚀(\ud83d\ude80)
UPDATE myusers SET name = "\ud83d\ude80 William" WHERE id = 8
The query in my table it will insert in the row name ud83dude80William, or ????William, or null. So in this time i have lost information in my row running that query
I have already tried to change the charset of my table, but it didn't make a difference
Finally to fix this issue (reffer to Akina answer, thanks very much) we have to add in the query double backslash.
Example:
UPDATE myusers SET name = "\\ud83d\\ude80 William" WHERE id = 8;
SELECT * FROM myusers;
I tried the following queries to change the column field of the table 'role' from 'Admin' to 'Administrator'. I am not informed about the values of id. my table 'role' contains two columns 'name' and 'id'.
None of the queries worked. Where am I going wrong?
update table role set name=replace('Administrator','Admin');
update table role set name='Administrator' where name='Admin';
select replace('Admin','Administrator') from role;
You don't use a table keyword so the second example is almost correct
update role set name='Administrator' where name='Admin';
The second would be the standard way:
update role -- "table" is not appropriate
set name = 'Administrator'
where name = 'Admin';
My guess is that you have no row that matches the condition -- presumably because of bad/hidden characters in name. Try this:
select *
from role
where name = 'Admin';
If this returns no rows, then try like:
where name like 'Admin%'
where name like '%Admin'
where name like '%Admin%'
Or maybe, using regexp:
where name regexp '[[:<:]]admin[[:>:]]'
(note that regexp is, by default, not case sensitive)
If one of these matches the row you want, then use it in the update.
you need to alter the table structure so the Mysql comand should be:
ALTER TABLE role CHANGE Admin Administrator [Admin column data type]
I have a table with words in spanish (INT id_word,VARCHAR(255) word). Lets suppose the table has these records:
1 casa
2 pantalon
If I search for the word pantalón (with a special char ó) it should not return any rows. How do I select exact matches only? It is currently returning the 2nd row.
SELECT * FROM words WHERE word='pantalón';
Thanks!
Solution from ifx, i changed the word field's collation to utf8_bin.
The reason this happens is down to the collation. There are collations that are accent sensitive (which you want in this case) and other that are accent insensitive (which is what you currently have configured). There are also case-sensitive and case-insensitive collations.
The following code produces the correct result:
create table test (
id int identity(1,1),
value nvarchar(100) collate SQL_Latin1_General_Cp437_CI_AS
)
insert into test values ('casa')
insert into test values ('pantalon')
select value collate SQL_Latin1_General_Cp437_CS_AS from test where value = 'pantalón'
The below code produces the incorrect result:
drop table test
go
create table test (
id int identity(1,1),
value nvarchar(100) collate SQL_Latin1_General_Cp437_CI_AI
)
insert into test values ('casa')
insert into test values ('pantalon')
select value collate SQL_Latin1_General_Cp437_CS_AS from test where value = 'pantalón'
The key here is the collation - AI means Accent-insensitive, AS means accent-sensitive.
i have this problem in our language too, so i did this, i have 2 coulmns for names, one of the i have named SearchColumn and the other one ViewColumn, when saving data I replace Special characters with other characters. when a user wants to search for something with the same function I do the changes and search it in the SearchColumn, if the search matches, I would display the value of the ViewColumn.
for example TableColumn could be contains value in forms New, new or NEW, how can I write query that returns only
SELECT * FROM myTable
WHERE myColumn = 'New'
but doesn't returns TableRows contains new or NEW
For MySQL, a simple option is:
SELECT * FROM myTable
WHERE myColumn = 'New'
AND BINARY(myColumn) = BINARY('New');
The second condition is logically sufficient, but makes the query slow if the table is big (the Index on myColumn cannot be used). The combination of the 2 conditions allows index use for the first condition, and then filtering out the non matching case.
You can use COLLATE in your where clause
SELECT *
FROM myTable
WHERE myColumn COLLATE latin1_general_cs = 'New'
The Best Way to make this column case sensitive is change this particular column if charset is UTF8 change it's collation to collate utf8_bin, after the modification of column it search case sensitive.
i.e I have a table name people with column name "name".
alter table people
modify column name varchar(50) charset utf8 collate utf8_bin;
Note : You can use varchar data type to varbinary, it works fine..
SELECT * FROM users WHERE BINARY userid = 'Rahul';
mysql does not check case of word so in the case of username or userid we shouold need to check case also for more security.
I hope this will help you