Grab a number in between a varchar in SQL - mysql

I am trying to grab a number by using SQL query. I need to grab the number before it says 'LEADS'
Sample of entry I might encounter:
PDIP300MIL-14LEADS
QFN6X6-40LEADS
QFN6X6-240LEADS
WSOIC/16LEADS
So as you can see the prefix can be any length. Also sometimes the delimeter is / or -. But it is fix that the suffix is LEADS.
On a sidenote. Other entries are like ICL7665 BCSA so it has no leads so it has to be skipped.
Edit: I am very sorry if I am not that clear. The one I am trying to grab is the number between the delimeter and Leads.
So in the four examples I am trying to grab: 14, 40, 240, 16.

You can do something like using substring_index
select
substring_index(
substring_index(
replace(col,'/','-')
,'LEADS'
,1),
'-'
,-1
)
from table1
DEMO
To skip entries you can filter result by using having clause
select
substring_index(
substring_index(
replace(col,'/','-')
,'LEADS'
,1),
'-'
,-1
) num
from table1
having num * 1 > 0
DEMO 2

https://dev.mysql.com/doc/refman/5.1/en/regexp.html
SELECT * FROM table WHERE field REGEXP '[0-9]+LEADS$'

Related

MySql Substring Index, Find and replace characters

I need to find the first and second "_" and extract whatever is between.
example data
doc_856_abc_123
doc_876_xyz_999
So far I have the following substring query. But I need help
select SUBSTRING_INDEX( column, '_', 2 )
It is outputting
doc_856
doc_867
How do I combine the above query to maybe another substring go get the desired results. Which would be.
856
867
Just apply SUBSTRING_INDEX again on the resulted string
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(column, '_', 2 ), '_', -1)

Count Characters after a certain pattern

I have a database that contains a column "Code" where the records have the following format "xx-xxx" and "xx-xx", for the later format i want to add a zero after the "-" to make it "xx-0xx", is there anyway to count the characters after a certain pattern in Mysql
Hmmm. If those are your only two possibilities, you can use case:
select (case when length(code) = 5
then replace(code, '-', '-0')
else code
end) as new_code
If you want to be more general, deconstruct the string and build it back again:
select concat_ws('-', substring_index(code, '-', 1),
lpad(substring_index(code, '-', -1), 3, '0')
)
Yes, you can use the CHAR_LENGTH(str) like this:
SELECT code,CHAR_LENGTH(SUBSTR(code,3))
from table

MySQL - extract number from data field using SUBSTRING

I'd like to extract the number between NUMBER and ;. So far I can extract the data up to the number, but I don't want anything after the number. e.g.,
SELECT
SUBSTRING(field, LOCATE('NUMBER=', rrule) + 7)
FROM table
Data field:
DATA:PASS=X12;NUMBER=331;FIELD=1
DATA:PASS=X12;NUMBER=2;FOO=BAR;FIELD=1
Desired Output:
331
2
You can use a combination of SUBSTRING_INDEX functions:
SELECT
SUBSTRING_INDEX(
SUBSTRING_INDEX(field, 'NUMBER=', -1),
';',
1)
FROM
tablename
Please see an example fiddle here.
The inner SUBSTRING_INDEX will return everything after the NUMBER= string, while the second will return everything before the ; returned by the inner function.

MYSQL Query to find the value

I've a table called PO_JOBS which contains a column called PO_NUMBER. The value should be in the format of PO_2014_JAN_1 (Prefix with PO_ , current year, three letters of month, and the last value is incremented).
For example,
The PO_Numbers for every month is
PO_2014_JAN_1
PO_2014_JAN_2
....
PO_2014_FEB_1
PO_2014_FEB_2
...
PO_2015_JAN_1
....
I've tried this
SELECT
CONCAT(('PO_'),
YEAR(CURRENT_TIMESTAMP),
'_',
SUBSTRING(UPPER(MONTHNAME(CURRENT_TIMESTAMP)),1,3),
'_',
IF(
LOCATE(
CONCAT(YEAR(CURRENT_TIMESTAMP),
'_',
SUBSTRING(UPPER(MONTHNAME(CURRENT_TIMESTAMP)),1,3)),
PO_NUMBER)>0,
MAX(CAST(SUBSTRING(PO_NUMBER,13) AS UNSIGNED))+1,
1))
FROM PO_JOBS
But it doesn't increment the value (ie) always return 1 (PO_2014_FEB_1). I hope you understand my problem.
My goal is to generate PO_NUMBER based on PO_current year_Three letters of current month_incremented value
Try the following query:
set #prefix := concat('PO_', year(current_timestamp),'_', SUBSTRING(UPPER(MONTHNAME(CURRENT_TIMESTAMP)),1,3), '_');
SELECT concat(#prefix, IFNULL(max(number), 0) + 1) AS next FROM
(
SELECT CAST( replace(PO_NUMBER,#prefix,'') AS UNSIGNED ) AS number
FROM PO_JOBS WHERE PO_NUMBER LIKE concat(#prefix,'%')
) AS numbers
I've solved this by the following query. Thanks to vadaica's helpful answer
SELECT
CONCAT(
('PO_'),
YEAR(CURRENT_TIMESTAMP),
'_',
SUBSTRING(UPPER(MONTHNAME(CURRENT_TIMESTAMP)),1,3),
'_',
IFNULL(MAX(CAST(
replace(PO_NUMBER,
concat('PO_',
year(current_timestamp),
'_',
SUBSTRING(UPPER(MONTHNAME(CURRENT_TIMESTAMP)),1,3),
'_'),'') AS UNSIGNED ))
+1,1)) AS number
from po_jobs
It sound like an auto increment problem. I think that from efficiency reasons it would be better to add columns of insertion \ update date and another row of auto increment value.
Another option on insertion would be to extract this number based on the number of lines in the table but it I see may troubles that can come out using this option.

Checking for doubled entries in value separated by commas?

Is there a way to get a value like this one: "300, 400, 500, 300" check each number separated with comma and if it is doubled delete it. So the value will look like this : "300, 400, 500".
I could do it in PHP script but I just wonder if it is possible using MySQL.
Create a temp table with unique index, insert values ignoring duplicate errors, select all records from the temp table, delete the table.
Quick play, but to get the unique values for each row you could use something like this
SELECT Id, GROUP_CONCAT(DISTINCT aWord ORDER BY aWord ASC)
FROM (SomeTable.Id, SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(concat(SomeColumn, ','), ' ', aCnt), ',', -1) AS aWord
FROM SomeTable
CROSS JOIN (
SELECT a.i+b.i*10+c.i*100 + 1 AS aCnt
FROM integers a, integers b, integers c) Sub1
WHERE (LENGTH(SomeColumn) + 1 - LENGTH(REPLACE(SomeColumn, ',', ''))) >= aCnt) Sub2
GROUP BY ID
This relies on having a table called integers with a single column called i with 10 rows with the values 0 to 9. It copes with up to ~1000 words but can easily be altered to cope with more
Probably easiest to use an INSERT / ON DUPLICATE KEY UPDATE to use this to make the values unique.