MySQL extract first 4 digits - mysql

I want to run a query in mysql which will return the record where the first 4 digits are '0123' or '0798' from the following column:
Number
0123 427 6465
0123 1451
01 23 46 47
0123 945675
07984 473456
0845 46 47
(012377) 5258
0800 586931
012 3668 6098
0 1238592371
I want the query to return all records where '0123' or '0798' are the first 4 numeric characters regardless of if there are other characters before or in between. E.g. I would want record 7 returned even though '0123' is in brackets. And I would want record 10 returned even though it is written as '0 123' i.e. there is a space in between.
Is regex relevant here? If so, what would the regex expression be?

Use a combination of LEFT and REPLACE.
REPLACE will strip out any unwanted brackets and whitespaces, and LEFT will select the first four characters, starting from left, of the newly formatted value which will be used in the WHERE clause selecting for values IN '0123', '0798'.
SELECT `number` FROM Numbers WHERE LEFT(REPLACE(REPLACE(REPLACE(`number`, '(', ''), ')', ''), ' ', ''), 4) IN ('0123', '0798')
Fiddle.
Result:
Number
0123 427 6465
0123 1451
01 23 46 47
0123 945675
07984 473456
(012377) 5258
012 3668 6098
0 1238592371
Also, it's worth noting, number is a Reserved Word in MySQL. I used backticks ` to escape it, however, it is advised that you do not use reserved words in your naming conventions.

We can use REGEXP_REPLACE function to remove all others characters other than number and get first four using the below query,
SELECT LEFT(REGEXP_REPLACE(Number, '[^0-9]+', ''), 4) as 4digitonly FROM Numbers a;
Please refer How to get only Digits from String in mysql?

Nothing is better than regex, yes they make us think even think recursivelly :)
Here is the query(of course it can be refactored N times):
SELECT n.number FROM Numbers n WHERE n.number REGEXP '^.*(0[ \t\r\n]*1[ \t\r\n]*2[ \t\r\n]*3).*|^.*(0[ \t\r\n]*7[ \t\r\n]*9[ \t\r\n]*8).*$'
Fiddle

Related

Sorting alphanumerically with a twist

I want to sort a field alphanumerically in the database. It turns out to be trickier than I thought. This is just example values, the content can vary, but I hope it's enough to get the idea.
I want to sort this list:
11
01
1
1A
01B
20a
01a
20
1b
2b
02a
Like this:
1
01
1A
01a
1b
01B
02a
2b
11
20
20a
Note that the relative ordering of equivalent numbers with and without leading zeroes is not important, it can be 1 01 or 01 1.
I've tried CAST(field AS UNSIGNED) but it doesn't work. Ideas?
If you're using MySQL 8.0 or higher, you can use REGEXP_SUBSTR(colname, '[a-z]+$') to get the alphabetic suffix, and CAST(colname AS UNSIGNED) to get the numeric prefix. Then you can sort by these.
SELECT code
FROM yourTable
ORDER BY CAST(code AS UNSIGNED), REGEXP_SUBSTR(code, '[a-z]+$')
See What is the equivalent of REGEXP_SUBSTR in mysql? for how to get similar functionality in earlier versions of MySQL.
Another possibility is:
ORDER BY CAST(code AS UNSIGNED), TRIM(LEADING '0' FROM code)

Need to ignore last two values in a lists column using Hive HQL

I have a column which contains all the values in lists.
Column A|Column B
AAA |1 2 45 67 89
BBB |16 25 36 45 89 63
CCC |52 63 98 41 22 66
Here in the above table, column B contains string values which are actually lists.
I need to ignore the first two and the last two values in Column B.
I tried using split function where i can ignore first two values. But ignoring last two values is the challenge as I have different sized lists.
The code which I used is:
select distinct column_A,column_B,split(column_B,'\\s')[2] AS ign_first_val,
split(column_B,'\\s')[-2] as ign_last_val
FROM Xyz
Is there any easy way to ignore first two and last two values in a list using HQL?
You should be able to use regexp_extract:
select regexp_extract(column_B, '^\\s*(\\d+\\s+){2}(.*?)(\\s+\\d+){2}\\s*$', 2)
The first part of the regex skips the first two values, and the last part skips the last two values, leaving just the middle part to be extracted into group 2 which is what is returned by the expression.
Here's a demo of the regex working on regex101.com

SQL Order By but invert for one element

I've got an SQL query which results upto the following
Code int1 int2 int3 S
C12 21 22 14 1
C33 43 56 2 3
C34 23 2 1 3
C55 33 92 12 5
CB56 45 66 10 5
MA10 10 11 12 1
This is the result of using OrderBy on Code
However I do not want it to order according to alphabets
But by the number after it for ex 1 in M1 and 33 in C33
In some cases the number after the alphabet may be 3 digits like E344
What I want it to look like
Code int1 int2 int3 S
MA10 10 11 12 1
C12 21 22 14 1
C323 43 56 2 3
C325 43 56 2 3
C34 23 2 1 3
C525 33 92 12 5
CB56 45 66 10 5
What I need is
M Should always show on top if Present
Then Sort According to Number on first place
Then Sort it according to the number on the Second place
Then sort it according to the number on the third place
The field 's' will always consist of the first digit from the Code
step 1 split up the column into 2 columns, 1 containing the letters, the other containing the numbers:
SELECT
substring(code,0,PATINDEX('%[0-9]%', Code)) as letters,
substring(code,PATINDEX('%[0-9]%', Code)) as numbers,
fields
FROM table
step 2 Convert the numbers to integer and sort
CONVERT(substring(code,PATINDEX('%[0-9]%', Code)),UNSIGNED INTEGER) as numbers
step 3 sort
Order by field1 asc, field2 desc... etc
It might be easier to use a subquery:
select * from
(SELECT
substring(code,0,PATINDEX('%[0-9]%', Code)) as letters,
CONVERT(substring(code,PATINDEX('%[0-9]%', Code)),UNSIGNED INTEGER) as numbers,
fields
FROM table) T
order by numbers asc, letters desc
This is conceptually simple: you want to order first by whether the code starts or not with the letter 'M', and then by the numeric portion of the code. You say in a comment:
extracting the first digit then order and then extracting the second
digit and then order and then extracting the third digit and then
order
This is exactly how alphabetical order has always worked. You order by first character; when it is the same you order by the second character, etc. so you need no special treatment for that case. Just get the numeric part of the code as a string, then order by it.
At this point, the only problem left to resolve is how to extract the numeric part of the code. That would be easy with PATINDEX() (as Alfons pointed out) but unfortunately MySQL does not support PATINDEX() as far as I know.
Now, what follows is extremely ugly, but it does work. Basically we get the non-numeric part of the string by removing all the numeric characters from it, then use the length of the non-numeric part to extract the numeric part.
SELECT mytable.* FROM mytable
INNER JOIN (
SELECT
code,
RIGHT(code, LENGTH(letters)) numbers
FROM (
SELECT
code,
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
code, 0, ''), 1, ''), 2, ''), 3, ''), 4, '')
, 5, ''), 6, ''), 7, ''), 8, ''), 9, '') letters
FROM mytable
) letters_table
) numbers_table
ON numbers_table.code = mytable.code
ORDER BY (numbers_table.code like 'M%') DESC,
numbers_table.numbers ASC
This solution is probably inefficient. However, I don't think you can get any acceptable efficiency anyway unless you store the numeric part on a separate column that you can index.
As you are guaranteeing that s is the value of the first digit in the code, this can be used to find the start of the code and from there, get the numeric part of the code. As you want to have all the codes starting with 'M' first, this results in the following ORDER BY clause:
... ORDER BY IF(SUBSTR(code, 1, 1) = 'M', 0, 1), SUBSTR(code, LOCATE(s, code))

query a telephone number mysql lots of formats

I'm trying to do a search where the last 5 digits of a number match out of 12
For example
text column "tel" contains "44 20 01122 4455" There maybe spaces there may not be spaces. It may not have a country code at the front, it may have a "0" and it could be a variable length from 13 digits to just 4 digits.
I would like to be able to match the following numbers and return the record.
44 20 01122 4455 (last 13 digits with spaces)
4420011224455 (last 13 digits no spaces)
020 01122 4455 (last 10 digits with spaces)
020011224455 (last 10 digits no spaces)
011224455 (last 9 digits)
224455 (last 6 digits)
24455 (last 5 digits)
4455 (last 4 digits)
The most common problem I will need to find is numbers stored as:
44 20 01122 4455 (last 13 digits with spaces)
4420011224455 (last 13 digits no spaces)
020 01122 4455 (last 10 digits with spaces)
020011224455 (last 10 digits no spaces)
but I only get 4455 or 24455 to search with
What mysql search query commands can I use?
You mean this:
SELECT TRIM(LEADING ' ' FROM RIGHT(LPAD(REPLACE(tel, ' ', ''), 13, ' '), 13))
This should replace all spaces with blanks. Then it will pad the string with spaces till its 13 characters long (in case its only 4 characters). Then it will trim the leading spaces it padded.
Would this work?
SELECT tel
FROM yourTable
WHERE RIGHT( REPLACE ( tel, ' ', ''), 5) = #param
The REPLACE() function removes any spaces. The RIGHT() function gets the last five characters.

MySQL append , prefix a range of numbers with 0

I have around a few thousand rows with which contain 3 digit numbers starting with 100 and ranging to 199 which i need to prefix with 0. There are also thousands of other numbers 4 digit numbers as well which i don't want to change.
I need find all the 3 digit numbers in the range and prefix only those ranging from 100 -199 with a 0 so as they are 4 digits eg 100 > 0100 , 104 > 0104 and so on.
Also these numbers may step eg 110 next is 124.
Is there a way I can do this using SQL? as i don't fancy changing these manually!
Many Thanks
This is best done with a programming language. That said, here's a SQL query that will update all the existing numbers:
UPDATE tableName SET fieldName = right(concat('0000',fieldName), 4) WHERE length(fieldName) < 4
The LPAD function is what you are looking for. You can use this in your query to pad the numbers on the fly.
SELECT LPAD(CONVERT(num AS CHAR), 4, '0') FROM tbl WHERE num > 99 AND num < 200
If you prefer to do this on the script side, str_pad will do the same in php.