Mysql sorting of alphanumeric values without considering alphabet - mysql

In database, it store values are
M2345
45
M345
E21
A3
is there a way to sort it correctly? like
A3
E21
45
M345
M2345

Assuming there can be at most one letter before the digits start, you could use a condition like this in your sorting definition:
ORDER BY CAST(IF(col REGEXP '^[a-z]', SUBSTRING(col, 2), col) AS SIGNED)
Unfortunately, MySQL doesn't have a replace function that can handle regular expressions, otherwise that would have been very helpful at this point.
You may also want to consider storing the numeric value itself in a separate calculated field for more efficient sorting.

Related

how to randomize column data and mask data using mysql

DISCLAIMER: NONE OF THESE VALUES ARE TRUE/REAL, ITS JUST A PRACTICE ASSIGNMENT
how to randomize the last 6 digits of the DBS account and obscuring the first 4 digits of the NRIC number with x using mysql. All values were keyed in manually and do not relate to each other.
Current
Desired Result
Just use substring operations:
SELECT
CONCAT('XXXX', SUBSTRING(NRIC, 5, 4)) AS NRIC,
Name,
Contact,
Salary,
CONCAT(SUBSTRING(DBS_Account, 1, 3), '-XXXXX-X') AS DBS_Account
FROM yourTable;
mysql rand function - https://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_round
using substring and concat
SELECT CONCAT(SUBSTRING(RAND(),3,5),'-',SUBSTRING(RAND(),3,1))
will give something like 40033-8
then concat with substring
set #a ='038-12645-6';
SELECT CONCAT(substring_index(#a,'-',1),'-',SUBSTRING(RAND(),3,5),'-',SUBSTRING(RAND(),3,1));
'038-63475-5'

MS SQL Query to remove prefixes

I have below like column values and would like to exclude the characters as well as the hyphen and only return digits. The replace function is not entirely helpful as sometimes the character length is 3 and sometimes its 4, see below as the digit length changes as well.
abc-1234567
sdfr-9876540
try-12345678
case-098765
If you want the part after the last hyphen, you can use substring_index():
select substring_index(col, '-', -1)
You can also extract the digits at the end using regexp_substr():
select regexp_substr(col, '[0-9]+$')

SQL BETWEEN case sensitivity

I'd like to create a BETWEEN parameter that returns values that are alphabetic (i.e. between A-Z and a-z inclusive). Is there a way to do this without using two BETWEEN clauses?
You don't say what platform you are using
WHERE UPPER(fieldname) BETWEEN 'A' and 'Z'
or
WHERE UCASE(fieldname) BETWEEN 'A' and 'Z'
you might want to get rid of some pesky spaces
WHERE UPPER(TRIM(fieldname)) BETWEEN 'A' and 'Z'
This will be slow, we can't use fieldname in an index if we run a function on it. So we are forcing a tablescan, which means two betweens and an or will be faster if fieldname is indexed and the table has some number of rows.

Find MySQL DB rows with a match in a pipe delimited column

I'm querying a table that has a column with member_ids stuffed in a pipe delimited string. I need to return all rows where there is an 'exact' match for a specific member_id. How do I deal with other IDs in the string which might match 'part' of my ID?
I might have some rows as follows:
1|34|11|23
1011
23|1
5|1|36
64|23
If I want to return all rows with the member_id '1' (row 1, 3 and 4) is that possible without having to extract all rows and explode the column to check if any of the items in the resulting array match.
MySQL's regular expressions support a metacharacter class that matches word boundaries:
SELECT ...
FROM mytable
WHERE member_ids REGEXP '[[:<:]]1[[:>:]]'
See http://dev.mysql.com/doc/refman/5.6/en/regexp.html
If you don't like that, you can search using a simpler regular expression, but you have to escape the pipes because they have special meaning to regular expressions. And you also have to escape the escaping backslashes so you get literal backslashes through to the regular expression parser.
SELECT ...
FROM mytable
WHERE member_ids REGEXP '\\|1\\|'
You can do this in one expression if you modify your strings to include a delimiter at the start and the end of the string. Then you don't have to add special cases for beginning of string and end of string.
Note this is bound to do a table-scan. There's no way to index a regular expression match in MySQL. I agree with #MichaelBerkowski, you would be better off storing the member id's in a subordinate table, one id per row. Then you could search and sort and all sorts of other things that the pipe-delimited string makes awkward, inefficient, or impossible. See also my answer to Is storing a delimited list in a database column really that bad?
'|' has a specific meaning in REGEXP. So suppose that the ids are separated by another delimiter like '~'.
Then you can run this code:
SELECT * FROM `t1`
where (Address Regexp '^1~') or
(Address Regexp '~1$') or
(Address Regexp '^1$') or
(Address Regexp '~1~')

how mysql ordering strings?

SELECT IF('y' = 'i', 1, 2 ) -> 1 why?
Can I change encoding or somethint to get it right? and how to order strings like irish and yes
now field and table encoded in utf8_lithuanian_ci
so how to order list with these characters?
You can compare/order those strings using BINARY operator -
SELECT * FROM table ORDER BY BINARY column;
From the reference - The BINARY operator casts the string following it to a binary string. This is an easy way to force a column comparison to be done byte by byte rather than character by character.
Alphabetic sort is performed with respect of the collation, so you have to find which is better for you.
http://dev.mysql.com/doc/refman/5.0/en/charset-general.html
Ordering works the same for strings as it does for integers. It performs an alphabetic sort.
SELECT * FROM table ORDER BY column ASC