How to search by specific pattern in MySQL? - mysql

I have below pattern of data in field
XXX-XX-XXX
Some of the data don't have that patterns.
So need to search those records.
SELECT * FROM table WHERE `name` NOT REGEXP '^.{10}$'
SELECT * FROM table WHERE `name` NOT REGEXP '^..........$'
Above 2 queries works fine. But not 100%.
Can I filter by {3}-{2}-{3} ?

You want to match a string with 3 alphanumeric chars followed with -, followed with 2 alphanumeric and then again a hyphen and 3 last alphanumeric in the string.
Use
'^[[:alnum:]]{3}-[[:alnum:]]{2}-[[:alnum:]]{3}$'
Details
^ - start of the string
[[:alnum:]]{3} - 3 alphanumeric chars
- - a hyphen
[[:alnum:]]{2}- - 2 alphanumeric chars and a -
[[:alnum:]]{3} - 3 alphanumeric chars
$ - end of string.
See MySQL "Regular Expression Syntax":
Character Class Name Meaning
alnum Alphanumeric characters

Related

Pad strings by spaces to certain length

Consider the following two strings,
source
-------
'ADAM' -- 4 chars length
'BOB' -- 3 chars length
I want to concatenate spaces after the strings where the number of spaces + length of string(n) should not exceed a specific number.
So the output should look something like the below where n = 8 in this case.
result
-----------
'ADAM ' -- 8 chars length
'BOB ' -- 8 chars length
How can I do this in mysql dynamically?
I could check the length of the field and use case statements for each scenario but thats not ideal. I am using mysql 8.0.17.
You would use the function rpad():
select rpad(name, 8, ' ')

MYSQL - Order column by ASCII Value

I have a column with texts, sorted by ASCII it should be ordered as:
- (hyphen)
0
1 (numbers)
2
A (uppercase)
B
_ (underscore)
a
b (lowercase)
c
However it is being ordered as:
- (hyphen)
0
1 (numbers)
2
a
b (lowercase)
c
A
B (uppercase)
C
_ (underscore)
How can I do the sorting by ASCII value?
The sort order is controlled by the collation. You can use the BINARY collation to sort by raw bytes, which in the case of ASCII data will cause it to sort by ASCII value. See https://dev.mysql.com/doc/refman/5.7/en/sorting-rows.html
SELECT ...
FROM mytable
ORDER BY BINARY mycolumn
This will be more flexible than using the ASCII() function because that function only returns the ASCII value of the first character. Using the BINARY collation allows sorting by the full string.
You could use ASCII:
SELECT *
FROM tab
ORDER BY ASCII(col_name) ASC

MySQL REXEXP doesn't work propertly

SELECT 'pharase' REGEXP '[^a-zA-Z]+' - it verifies that the phrase does not contain English chars (or not?).
Why is query SELECT '123g' REGEXP '[^a-zA-Z]+' -> 1 show 1 instead 0?
To check if a string only contains English chars, use
SELECT 'pharase' REGEXP '^[a-zA-Z]+$'
where:
^ - asserts the position at the start of the string
[a-zA-Z]+ - 1 or more ASCII letters
$ - the end of string
SELECT '123g' REGEXP '[^a-zA-Z]+' -> 1 shows 1 because the string contains 123, non-letters, a partial match is found with REGEXP (unlike LIKE that requires a full string match).

mysql where string ends with numbers

My table column contains values like:
id | item
-------------
1 | aaaa11a112
2 | aa1112aa2a
3 | aa11aa1a11
4 | aaa2a222aa
I want to select only rows where value of item ends with numbers.
Is there something like this?
select * from table where item like '%number'
You can use REGEXP and character class
select * from table where item REGEXP '[[:digit:]]$'
DEMO
Explanation:
[[:digit:]] >> Match digit characters
$ >> Match at the end of the string
Within a bracket expression (written using [ and ]), [:character_class:] represents a character class that matches all characters belonging to that class.
SIDENOTE:
Other helpful mysql character classes to use with REGEXP, taken from the documentation:
Character Class Name Meaning
alnum Alphanumeric characters
alpha Alphabetic characters
blank Whitespace characters
cntrl Control characters
digit Digit characters
graph Graphic characters
lower Lowercase alphabetic characters
print Graphic or space characters
punct Punctuation characters
space Space, tab, newline, and carriage return
upper Uppercase alphabetic characters
xdigit Hexadecimal digit characters
you can use REGEXP
select * from table where RIGHT(item ,1) REGEXP '^-?[0-9]+$';
Yes you can use like for numbers.
select * from table where item like '%1'
This will work

substring dynamic number of characters

I'm working with 2 sets of data that were merged together, but they're inconsistent in their format. Some are 10 characters, all numbers. Others may have a separator : at position 4. I need to substring the first 4 characters. But if the 4th character is a : substring only the first 3 characters.
Does mysql have an IF functionality to determine the number of characters to substring based on the character in position 4?
select substring(id, 1 , 3/4) from table1
You can treat the field like it's colon separated and do this to select only the first part:
SELECT SUBSTRING_INDEX(id, ':', 1)
See also: SUBSTRING_INDEX()