When I filter numeric id with where condition inside with key all then result is like this which is correct https://prnt.sc/1ubkviy
But when we pass alpha numeric value inside where condition, still it is giving result then can you explain us in detail why this is happening https://prnt.sc/1ubkzbq
Try using BINARY for exact match:
SELECT * FROM office_category WHERE BINARY id="2sssss"
Related
This question is regarding mysql. I want to get the only characters value of min and max range. I think this is explained better with an example.
Example- The column of table is username like abc123 output should be 3,12de:-o/p->2,erogan44e :o/p->7 etc.
i want the select only characters length between min to max.
Is it possible without using procedure ?
Should we use regular expression ?
To count the number of alphabets in a String, you first need to replace all the non-alphabet characters with an empty space and then calculate the length, e.g.:
SELECT LENGTH(REGEXP_REPLACE(column_name, '^[A-Za-z]', '')) AS value
FROM table
ORDER BY value DESC;
The problem with this approach is, MySQL does not have REGEXP_REPLACE function (unlike MariaDB). So, you will have to write your own function for this, have a look at this example.
Is there any way to get value from JSON format which are stored in database field.
I had tried like below but sometimes it return wrong value and it doesn't seems to correct thing to fetch records.
Actually I am trying to addition of all sizes
SELECT
substring_index(substr(size,locate('"xs":"',size)+char_length('"xs":"')),'"',1)+
substring_index(substr(size,locate('"s":"',size)+char_length('"s":"')),'"',1)+
substring_index(substr(size,locate('"m":"',size)+char_length('"m":"')),'"',1)+
substring_index(substr(size,locate('"l":"',size)+char_length('"l":"')),'"',1)+
substring_index(substr(size,locate('"xl":"',size)+char_length('"xl":"')),'"',1)+
substring_index(substr(size,locate('"xxl":"',size)+char_length('"xxl":"')),'"',1)+ substring_index(substr(size,locate('"xxxl":"',size)+char_length('"xxxl":"')),'"',1)+
substring_index(substr(size,locate('"xs\/s":"',size)+char_length('"xs\/s":"')),'"',1)+ substring_index(substr(size,locate('"m\\/l":"',size)+char_length('"m\\/l":"')),'"',1)+
substring_index(substr(size,locate('"xl\\\/xxl":"',size)+char_length('"xl\\\/xxl":"')),'"',1)
as qty FROM `table_name`
I have also taken the reference to catch numeric value from a string from below question of StackOverflow:
How do you extract a numerical value from a string in a MySQL query?
it returns correct value and i can do addition addition of return value,,but for 2 digit number it is not good for me (like if it return 15 so it is 6 for my case)
SELECT ID FROM REPORT where ID NOT LIKE '%-G'AND ID NOT LIKE '%-H';
The above query isnt returning what I expect. It is returning 5 row sets when it should 6. 6 ID matches the condition of query. Do I need to use a regular expression to get the right row sets? I have tried '%\-G' with single backslash and double backslash '%\\-G' both return the desired 6 row sets. Which should it be?
Sample data:
87878
54545
21545-G
45487
45454
45458
78741
23232-H
'%\-G' this is the correct way of doing it and not '%\\-G'
For further reference follow : MYSQL Escape Sequences
I need to search for a value like 1234-abc. The database doesn't have this particular value, but has another value 1234. Now the problem is when I write my query like
SELECT * FROM words WHERE tval='1234-abc'
instead of fetching an empty recordset, it fetches the 1234 value, it seems to ignore anything after the -, any idea what's going on?
http://sqlfiddle.com/#!2/9de62/3
You can use the BINARY keyword for the exact match
SELECT tval FROM words WHERE BINARY tval='1223-abc';
Binary is a built-in keyword that after your WHERE clause that forces a comparison for an exact case-sensitive match
Fiddle
The existing expression is implicitly converting the string expression to a number - you need to explicitly convert the number to a character strng, like so:
SELECT tval FROM words WHERE convert(tval,char(20))='1223-1ABCDE';
SQLFiddle here.
I have a table with a field value which is a varchar(255). The contents of the field can be quite varied:
$1.20
$2994
$56 + tax (This one can be ignored or truncated to $56 if necessary)
I have a query constructed:
SELECT value FROM unnamed_table ORDER BY value
However, this of course uses ASCII string comparison to order the results and does not use any numerical type of comparison.
Is there a way to truly order by value without changing the field type to DECIMAL or something else? In other words, can the value field be modified ('$' removed, value converted to decimal) on the fly before the results are sorted?
You could sort on an expression made to "parse" the text into decimal
SELECT value FROM unnamed_table ORDER BY expression_returning_decimal(value)
Where your expression uses MySQL functions to extract the number from the string depending on what form you expect it to take (something like CAST(value AS DECIMAL(10,2)), but you'll probably need to deal with the extraneous non-numeric characters somehow as I'm not sure what the CAST will give if you don't strip them).
Create a second column without the $-sign, sort on that one and use the data of the original column in your application.
In order to create the helper column and sort on it you would need something like this:
SELECT value, CAST(SUBSTR(value, 2) AS UNSIGNED) sort_col
FROM unnamed_table ORDER BY sort_col