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
Related
I have a column that consists of details of an orderline named 'ConcatValue'. An example of a value in this column is:
573856014/100/M00558640/OrderQty12
I want to extract the order value which can be founded after 'OrderQty'. I thought I had a solution by executing the following statement: substr(ConcatValue,char_length(ConcatValue)-1,char_length(ConcatValue))
This results in only level the last 2 characters of the string from the column ConcatValue. For the ConcatValue mentioned above I will get the following result: '12'. Which is the desired result.
But when the orderline has an Order quantity below 10, for example in the following ConcatValue:573856014/100/M00558640/OrderQty3
I will get the following result: y3
My question: Is there a way to delete 'y' if a row has an y within the value? Or is there a way to replace the y with a 0? Or is there a way to only select the last digits from the ConcatValue string?
Use string functions.
With substring_index() you can get the last part of the string and with replace() remove 'OrderQty':
select replace(
substring_index(ConcatValue, '/', -1),
'OrderQty',
''
)
from tablename
Actually, the simplest method is simply:
select substring_index(ConcatValue, 'OrderQty', -1)
I have strings which have a JSON-like format, including:
..."id":"500", ..., "id":"600", ...
I need to parse the second id out of the column. I found lots of answers using substring_index, however, I need to get the string after the 2nd (of potentially n) occurrences and not the string before to parse out the ID.
Is there a nice solution?
To find the substring of a column "some_column" occurring after the nth
occurrence of a target string,
SELECT
SUBSTRING(some_column, CHAR_LENGTH(SUBSTRING_INDEX(some_column, <target_string>, <n>)) + <length of target string + 1>)
FROM some_table
-- or if you want to limit the length of your returned substring...
SELECT
SUBSTRING(some_column, CHAR_LENGTH(SUBSTRING_INDEX(some_column, <target_string>, <n>)) + <length of target string + 1>, <desired length>)
FROM some_table
For this question, the form would be:
SELECT
SUBSTRING(col, CHAR_LENGTH(SUBSTRING_INDEX(col, '"id":"', 2)) + 7)
FROM `table`
For now I have:
SELECT substring_index(
substr(col, locate('"id":"', col, locate('"id":"', col) + 6) + 6),
'"',
1)
FROM table
Would love to see a "nicer" answer :-)
In Snowflake, this can be done as follows:
select
, split_part([field_name], '{separator}', {n-counter})
from
[table]
Note: {separator} and {n-counter} are inputs provided by the user. Snowflake requires apostrophes around {separator}.
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.
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.
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?