Determine CaseSensitive in WHERE clause - mysql

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

Related

ALTER COLUMN TYPE from tinyInt to Varchar in Mysql

I need to change column type from tinyInt(used as bool) to Varchar, without loosing data.
I have found many answers on stack-overflow but all of them are written in postgres and I have no idea how to rewrite it in Mysql.
Answers for this problem on stack-overflow looks like that:
ALTER TABLE mytabe ALTER mycolumn TYPE VARCHAR(10) USING CASE WHEN mycolumn=0 THEN 'Something' ELSE 'TEST' END;
How would similar logic look like in Mysql?
The syntax you show has no equivalent in MySQL. There's no way to modify values during an ALTER TABLE. An ALTER TABLE in MySQL will only translate values using builtin type casting. That is, an integer will be translated to the string format of that integer value, just it would in a string expression.
For MySQL, here's what you have to do:
Add a new column:
ALTER TABLE mytable ADD COLUMN type2 VARCHAR(10);
Backfill that column:
UPDATE mytable SET type2 = CASE `type` WHEN 0 THEN 'Something' ELSE 'TEST' END;
If the table has millions of rows, you may have to do this in batches.
Drop the old column and optionally rename the new column to the name of the old one:
ALTER TABLE mytable DROP COLUMN `type`, RENAME COLUMN type2 to `type`;
Another approach would be to change the column, allowing integers to convert to the string format of the integer values. Then update the strings as you want.
ALTER TABLE mytable MODIFY COLUMN `type` VARCHAR(10);
UPDATE mytable SET `type` = CASE `type` WHEN '0' THEN 'Something' ELSE 'TEST' END;
Either way, be sure to test this first on another table before trying it on your real table.

MySQL SELECT row based on prefix

Im attempting to do a SELECT on data in a table that contains prefixes, and I have the "keyword".
So unlike a normal search where the prefix/keyword is contained in the row data, and that the prefix/keyword is shorter (or contained in) a possible row of data.
This is the opposite. If have prefixes in rows and I want to find the best matching row based on the (longer) word/phase.
CREATE TABLE table1 (id INT NOT NULL AUTO_INCREMENT, keyCode VARCHAR(45) NULL, Username VARCHAR(45) NULL, PRIMARY KEY (id));
INSERT INTO table1(keyCode,Username)VALUES('123','Peter')
INSERT INTO table1(keyCode,Username)VALUES('456','Paul')
INSERT INTO table1(keyCode,Username)VALUES('1234','John')
Now let's say the phrase I have been given is longer than the prefix in the data.
Like this:
SELECT * FROM table1 WHERE keyCode LIKE '123456%';
I know this will not work, but I would like to return the Row with User 'John'.
How?
(I can use stored procedures)
You may phrase your LIKE expression in the reverse order:
SELECT *
FROM table1
WHERE '123456' LIKE CONCAT(keyCode, '%');
This would compare, for example, '123456' against '1234%', which should be a match in this case.
You can check for the existence of the keyCode prefix at the beginning of your string using LOCATE(). For example:
SELECT *
FROM table1
WHERE LOCATE(keyCode,'123456') = 1;

Replace the text of whole column it contains specific string mysql

I have a question in mysql about searching the column in a table and if it contains specific string replace whole column with particular text.
Lets say If the column containing this textdata/variant_image/2/IW_HH_1B_sxph-2o_l7ar-4r_kh2k-fd.jpg has this string in itIW_HH_1B_ then replace it with data/variant_image/1/IW_HH_1B.jpg
I tried update,replace functions but it didnt work. And searching on google returns only replacing that particular string option. but not complete column text.
A simple update works:
UPDATE your_table
SET your_column = 'data/variant_image/1/IW_HH_1B.jpg'
WHERE your_column LIKE '%IW_HH_1B_%'
You can use a conditional update for that:
UPDATE tablename
SET columnname = 'data/variant_image/1/IW_HH_1B.jpg'
WHERE LOCATE('IW_HH_1B' COLLATE utf8_bin, columnname) > 0
If you want it to be case insensitive leaf the collate part out.

mysql queryto list only uppercase row from database

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.

SQL - how to return exact matches only (special characters)

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.