MySQL: Select values containing only one digit - mysql

Let's say I have a column with emails:
test1#test.com
test2#test.com
test3test#tes.com
test123#test.com
test321#test.com
test23test.com
How can I select only those that contains only one digit?
Result should be:
test1#test.com
test2#test.com
test3test#tes.com
I tried:
REGEXP '[[:digit:]]{1}' and REGEXP '[0-9]{1}' but it shows all results that contain AT LEAST one digit

try to use ^[^0-9]*[0-9]{1}[^0-9]*$

If you don't specify number of repetitions, it is one copy by default.
Try something like
REGEXP '(^[0-9]+)([0-9])#.+'

Related

Select a value using iif access query

I have a row who contains this value :
account :
adm.ahrgrst001
adm.ns2dhdujhd
adm.ff2hdjhh
adm.haidhidh103
adm.hshiksh122
adm.cn3ehuioe
i want to extract two different values:
when it ends like adm.hshiksh122 i want to extract hshiksh
and with start with adm.cn3ehuioe i want ehuioe
both without the adm. at the beginning
I have thinked this
IIF(isnumeric(RIGHT (account,3)),LEFT(account,LEN(account)-4),RIGHT (account,LEN(account)-7))
the value that are like adm.cn3ehuioe i got wrong like adm.cn3ehui
and adm.ahrgrst001 is correct ahrgrst
Thanks to everyone who will read
The correct way to get the 2 types of values is with:
account LIKE 'adm.[!0-9][!0-9]#[!0-9]*'
for the values that have 2 letters, 1 digit and letters after the dot, and:
account LIKE 'adm.*###'
for the values that end in 3 digits.
So use this:
SELECT IIF(
account LIKE 'adm.*###' ,
MID(account, 5, LEN(account) - 7),
MID(account, 8)
) AS result
FROM tablename
WHERE account LIKE 'adm.*###' OR account LIKE 'adm.[!0-9][!0-9]#[!0-9]*'
If there are no other values than these 2 types then you may remove the WHERE clause.
Results for your sample data:
result
ahrgrst
dhdujhd
hdjhh
haidhidh
hshiksh
ehuioe

List all value in a column which is having 5 consecutive number, it can have any other characters as well before or after the sequence

i need only those results which is having at least one set of 5 consecutive digits in it.
This was my original query
SELECT [Field]
FROM [testPackage].[dbo].[Table_1]
where Field like '%[0-9]{5}%'
Below is the table
Field
---------------------
fhjsfh4324kjkjk
45654rewrwejug
g,nerht54535fjklrejltkj
fhdjfhjh425435
hjlwrjtljr424556fslfj
kljrkj67587598347rerjwlej
esd980rewrkw456
the query should list only
45654rewrwejug485345
g,nerht54535fjklrejltkj
Just continue in the direction you were already heading, and repeat [0-9] five times in sequence inside the LIKE expression:
SELECT *
FROM [testPackage].[dbo].[Table_1]
WHERE Field LIKE '[0-9][0-9][0-9][0-9][0-9][a-zA-Z,]' OR
Field LIKE '[a-zA-Z,][0-9][0-9][0-9][0-9][0-9][a-zA-Z,]' OR
Field LIKE '[a-zA-Z,][0-9][0-9][0-9][0-9][0-9]' AND
Field NOT LIKE '%[0-9][0-9][0-9][0-9][0-9][0-9]%'

Mysql select to get only part of a row from a column

I have a table with a column that contains a string of numbers and I only want to return the last couple of digits.
For example:
column1 | column2
_________________
Blah | 1231357
I need a select that will return the last couple of digits from the second column.
Use the RIGHT function:
SELECT RIGHT(column2, 3) AS LastDigits FROM TableName
Change 3 to the number of digits you want.
A modulus operator will take only the last two digits.
SELECT MOD(column2, 100) FROM mytable
Change 100 to 1000 to get three digits, etc.

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.

using regualr expression in mysql to select specific rows

I'm trying to select a small set of records that match a patten I have a series of numbers in each row such as
1
2
3
some of them have sub numbers
3.1
3.2
4
5
I can select only the whole numbers using
REGEXP '^[0-9]+$'
I can select all rows that have a . in them like 3.1 3.2 etc using
REGEXP '[.]{1}'
but I can't seem to select for example only sub numbers that start with 3 I've tried
REGEXP '[^3.]{1,}'
but that returns all records
Ideally I want to return only records that are in the format of 3.1 I would like to define the start number and the dot so 3. then the second part match against the records
I hope this makes sense
I used '3\.[0-9]{1,}' - it matched.
Yours probably fails because of unescaped dot - ., which matches every character.
Escape characters with \
Format 3.d where d is digit:
3\\.[0-9]