mysql where string ends with numbers - mysql

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

Related

How to search by specific pattern in 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

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).

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()

Mysql get values from column having more than certain characters without punctuation

How can I get all the values from mysql table field, having more than 10 characters without any special characters (space, line breaks, colons, etc.)
Let's say I have table name myTable and the field I want to get values from is myColumn.
myColumn
--------
1234
------
123 456
------
123:456
-------
1234
5678
--------
123-456
----------------
1234567890123
So here I would like to get all the field values except first one i.e. 1234
Any help is much appreciated.
Thanks
UPDATE:
Sorry if I was unable to give proper description of my problem. I have tried it again:
If there is count of more than 10 characters without punctuation, then retrieve that as well.
Retrieve all the values which have special characters like line break, spaces, etc.
Yes, I have primary key in this table if this helps.
The logic seems to be "more than 10 characters OR has special punctuation":
where length(mycol) > 10 or
mycol regexp '[^a-zA-Z0-9]'
SELECT MyColumn
From MyTable
WHERE MyColumn RLIKE '([a-z0-9].*){10}'
[a-z0-9] matches a normal character.
([a-z0-9].*) matches a normal character followed by anything.
{10} matches the preceding regexp 10 times.
The result is that this matches 10 normal characters with anything between them.