MYSQL Query to find the value - mysql

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.

Related

find index position in given string by using sql

My value is "asdsdf2739173sidfsd"
Here I want to get first and last occurrence of any number position or index in given string, please help me
To find the indices of the first and last occurrence of a number in a string, we can try using REGEXP_REPLACE:
WITH yourTable AS (
SELECT 'asdsdf2739173sidfsd' AS val
)
SELECT
val,
LENGTH(val) - LENGTH(REGEXP_REPLACE(val, '^[^0-9]*', '')) AS idx_first,
LENGTH(REGEXP_REPLACE(val, '[^0-9]*$', '')) - 1 AS idx_last
FROM yourTable;
Demo

How to sort the string on the basis of numbers?

I am working on the sql query in which I want to sort the string on the basis of numbers.
I have one column (Column Name is Name) table in which there are multiple fields. On using ORDER BY NAME, it prints in the following way:
hello_world
hello_world10
hello_world11
hello_world12
hello_world13
hello_world14
hello_world15
hello_world4
hello_world5
For the above query, I have used ORDER BY NAME; but it doesn't seem to print on the basis of numbers.
Problem Statement:
I am wondering what sql query I need to write or what changes I need to make in my sql query above so that it prints everything on the basis of numbers, the o/p should be this:
hello_world
hello_world4
hello_world5
hello_world10
hello_world11
hello_world12
hello_world13
hello_world14
hello_world15
you want a numeric ordering, then you need to create a numeric value to order on.
currently you have strings.
if the pattern is true, then you can use a combination of string manipulation to trim off the first characters, which should leave only numbers, then use TO_NUMBER() to convert for the ordering
something like
select name
from mytable
order by to_number( replace( name, 'hello_world','' ))
I think the simplest solution for this particular case (where all the values have the same prefix) is:
order by length(name), name
Try this:
SELECT name,
CASE WHEN REGEXP_INSTR(name, '[0-9]') = 0 THEN 0
ELSE CAST(SUBSTR(name, REGEXP_INSTR(name, '[0-9]')) AS INT)
END AS progressive
FROM my_table
ORDER BY progressive;
we can order it using replace and cast methods.
I tried the following query
select Name, cast(REPLACE(Name, 'hello_world', '') as UNSIGNED ) as repl from Users order by repl;
To generage sample data
CREATE TABLE Users (
Name varchar(255) NOT NULL
);
insert into Users(Name) values
('hello_world'),
('hello_world4'),
('hello_world5'),
('hello_world10'),
('hello_world11'),
('hello_world12'),
('hello_world13'),
('hello_world14'),
('hello_world15')
;
EDIT
query without replaced column,
select City from Persons order by cast(REPLACE(City, 'hello_world', '') as UNSIGNED );
Though the question is about mysql.
I tried in sql server.
create table #t1 (id varchar(100));
insert into #t1 (id) values ('Pq1'),('pq3'),('pq2')
select * from #t
order by
CAST(SUBSTRING(id + '0', PATINDEX('%[0-9]%', id + '0'), LEN(id + '0')) AS INT)

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

Grab a number in between a varchar in SQL

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$'

select distinct substring values of a field and count the number of instances of a char in that selection

I'm trying to select distinct substring values of a field and count the number of instances of a char in that selection.
I've found this wonderful post which answers half of it.
So, so far, i can count the instances of a char in my field, it works great. Now the even harder part, what if i select a piece of string using :
SELECT DISTINCT SUBSTRING_INDEX(my_field, '-', -1) AS chunk
In this case i'm only selecting the last part of the string (everything after the last'-'). How can i apply this formula to chunk (trying to count the number of instances of '_' in the new string ? :
(LENGTH(chunk) - LENGTH(REPLACE(chunk, '_', ''))) / LENGTH('_')
I know i shoud be using HAVING to make operation on chunk as it's not a real field, but how can i do something like :
SELECT DISTINCT SUBSTRING_INDEX(my_field, '-', -1) AS chunk, (LENGTH(chunk) - LENGTH(REPLACE(chunk, '_', ''))) / LENGTH('_') AS total FROM my_field HAVING total < 2
The problem here is that i can't use 'chunk' in the last part since it's not a field..
The problem here is that i can't use 'chunk' in the last part since it's not a field..
Replace 'chunk' in the last part with
SUBSTRING_INDEX(my_field, '-', -1)
Don't know what's the problem?