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

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.

Related

Split value and place separated value into two different column using mysql

I want to split two values which is separated by multiplication symbol for example "12X36" after split it should get 12 in one column 36 in one column i.e 'X' symbol removed from those numbers get get final values in separate column for every record in Mysql
SELECT SUBSTRING_INDEX('12X36','X',2)
but this is eliminating 'X', I want to show 12 in separate column and 36 in separate column but don't know further step
expected output:
1) 12X36
2) 23X40
column1 column2
12 36
23 40
getting output
SUBSTRING_INDEX('12X36','X',2)
12
As mentioned, many ways. Here is one using LEFT and RIGHT
SELECT LEFT('12X36', LOCATE('X', '12X36') - 1), RIGHT('12X36', LOCATE('X', '12X36') - 1)
and one using reg exp, here the substrings are all digits from the start (denoted by ^) up to a non-digit and all digit between the last noon-digit and the end of the string (indented by $)
SELECT REGEXP_SUBSTR('12X36', '^[0-9]*'), REGEXP_SUBSTR('12X36', '[0-9]*$')
There are numerous ways. One is to locate() the 'X' and substring() around it.
SELECT substring('12X36', 1, locate('X', '12X36') - 1),
substring('12X36', locate('X', '12X36') + 1)

Treat number as Zero in mysql query

If have rows table like this
id content
1 this five lenght is 12345
2 this five lenght is 23456
3 this six lenght is 234567
4 this six lenght is 238567
Then when I want to select and group by content, it will result
SELECT * FROM table GROUP BY content
1 this five lenght is 00000
3 this six lenght is 000000
Is there a way to achieve this, that can replace number with zero in mysql query?
Many thanks in advance.
Replace numbers with zero and then group them.
Example for replacement:
Select REGEXP_REPLACE('Stackoverflow 2456','[0-9]','0')
Stackoverflow 0000
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=db93de9c1c965090f46b4dbb1f48a63e
In your case:
SELECT REGEXP_REPLACE(CONTENT, '[0-9]','0') FROM TABLENAME GROUP BY REGEXP_REPLACE(CONTENT, '[0-9]','0')
Be careful tho this will probably do a full TABLE scan, so it will be really slow on large tables.
You can try with REGEXP_REPLACE() function - it'll work on mysql 8+ version
SELECT id, REGEXP_REPLACE (content,'[0-9]', '0') as content
FROM tablename GROUP BY REGEXP_REPLACE (content,'[0-9]', '0')

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 View decimal place

I have a MySQL view called Balance created from 2 tables order and income with PHPMyAdmin and contains some calculated fields ex: CustomerBalance the decimal place become automatically 8, I mean the field Type is decimal(50,8)
How can i make it 2 only ?
You can use truncate
SELECT TRUNCATE(1.999,2);
return 1.99
select TRUNCATE(your_column,2) from your_table;
In the select list where you calculate the CustomerBalance expression, explicitly truncate or round (depending on your requirements) the result to 2 digits:
select ... round(..., 2) as CustomerBalance ...

Comparing number in formatted string in MySQL?

I have a PolicyNo column in my table in MySQL with a format like this:
XXXX-000000
A four capital-case characters followed by a dash and a six digit number.
The six digit number is incremental, adding 1 for the next row, and the the four characters is always the same for all rows. The PolicyNo column is unique with a type of varchar(11).
If ordered, it will look like this:
XXXX-000001
XXXX-000002
XXXX-000003
...
Now I want to get all PolicyNo whose number is greater than a specified number.
For example: Retrieve all PolicyNo greater than 'XXXX-000100':
XXXX-000101
XXXX-000102
XXXX-000103
...
I test this query and it works fine, but I just didn't know if it is really safe to do such:
SELECT 'XXXX-000099' > 'XXXX-000098'
, 'XXXX-000099' > 'XXXX-000100'
, 'XXXX-000099' > 'XXXX-000101'
Result:
+-------------------------------+-------------------------------+-------------------------------+
| 'XXXX-000099' > 'XXXX-000098' | 'XXXX-000099' > 'XXXX-000100' | 'XXXX-000099' > 'XXXX-000101' |
+-------------------------------+-------------------------------+-------------------------------+
| 1 | 0 | 0 |
+-------------------------------+-------------------------------+-------------------------------+
Is there any other way to do this or is it already OK to use this?
Because your numbers are zero padded, as long as the four letter prefix is the same and always the same length, then this should work as MySQL will do a lexicographical comparison.
Note that one less 0 in the padding will cause this to fail:
SET #policy1 = 'XXXX-00099';
SET #policy2 = 'XXXX-000598';
SELECT #policy1, #policy2, #policy1 > #policy2 AS comparison;
=========================================
> 'XXXX-00099', 'XXXX-000598', 1
If you need to truly compare the numbers at the end, you will need to parse them out and cast them:
SET #policy1 = 'XXXX-00099';
SET #policy2 = 'XXXX-000598';
SELECT #policy1, #policy2,
CONVERT(SUBSTRING(#policy2, INSTR(#policy2, '-')+1), UNSIGNED) >
CONVERT(SUBSTRING(#policy2, INSTR(#policy2, '-')+1), UNSIGNED) AS comparison;
=========================================
> 'XXXX-00099', 'XXXX-000598', 0
You can also use SUBSTRING function provided by MySQL, like the following query.
SELECT count(*) FROM Table1 where substring(policyNo,6)>YOUR_RANGE;
here 6 is passed as the 6 digit number start from 6th position And if you do want to pass initial 4 charecter as well then you can use following query. Here second where clause will take intial 4 letters from the policyNo.
SELECT count(*) FROM Table1 where substring(policyNo,6)>YOUR_RANGE AND substring(policyNo,1,4) = 'ABCD'