How to Extract Numeric Range from Text in SQL - regex

I am fairly new to SQL and I'm trying to extract some data from an ORACLE DB. My goal is to return rows where the query value lies in the range speicified by the "AA_SYNTAX" column in a table.
For example:
If the "AA_SYNTAX" column is 'p.G12_V14insR' and my search value is 13, I want to return that row. This column is organized as p.%number_%number%. So basically I want to return the two numerical values from this column and see if my query value is between them.
I have all the tables I need joined together and everything, just not sure how to construct a query like this. I know in regex I would do something like "\d+" but im not sure how to translate this into SQL.
Thanks

Using Oracle, you can use Regular Expressions to extract a number from the string.
More specifically, I would look into REGEXP_SUBSTR.
Using the date given in your example above, you could use:
with cte as
(
select 'p.G12_V14insR' as AA_SYNTAX from dual
)
select
REGEXP_SUBSTR(AA_SYNTAX,'p\.[[:alpha:]]+([[:digit:]]+)', 1, 1, NULL, 1) as Bottom
,REGEXP_SUBSTR(AA_SYNTAX,'\_[[:alpha:]]+([[:digit:]]+)', 1, 1, NULL, 1) as Top
from cte
I'm sure you could clean up the Regular Expression quite a bit, but, given this, you get the value of 14 for Top and 12 for Bottom.
Hope this helps move you in the right direction.

Related

if and search function on power BI token literal expected

I have a table called "food suppliers" with a column called "sites" that have multiple countries ending in ".com", ".es", ".co.uk".
I want to create a new column that separates these sites into their corresponding country names using the if and search function on power query.
so far in power query custom column I have:
Country = IF (SEARCH ("*.com", foodsuppliers[sites],,0) = 0, IF (SEARCH ("*.es", foodsuppliers[sites],, 0)= 0, "Spain","UK"),"USA")
But I am getting a "token literal expected" under the first = sign in "IF (SEARCH ("*.com", foodsuppliers[sites],,0) = 0"
does any one have ideas why or a better way to run this code on power query/power bi?
thanks.
Nothing in your code seems relevant
There is no SEARCH function, you want to use Text.Contains
You cant write foodsuppliers[sites] or you will be getting the entire column of all rows. You probably want each current row, which you would get with [sites]
This is not excel where you can do =if (xxx,fff,zzz) the format is if x then y else z
I recommend some tutorials

Conditionally Filtering Out Rows based on 2 Parameters in w/ Power Query

I have a table similar to the one attached below:
What I would like to do, using power query, is conditionally filter out (remove) rows where CenterNum = 1101 and DepCode = 257. I figured Table.SelectRows() would work but it doesn't and the query just returns, this table is empty. The #"Expanded AccountLookup" ,in my formula below, is referencing the power query applied step before the one I am trying to create. I'm hoping to get some input on how to remove rows based on these two paramters.
= Table.SelectRows(#"Expanded AccountLookup", each [CostCenterNumber] = "1111001" and [NoteTypeCode] = "257")
Thank you!
You didn’t post a screenshot so it is hard to tell if the column format is text or numerical but try removing the quotes around the numbers
= Table.SelectRows(#"Expanded AccountLookup", each [CostCenterNumber] = 1111001 and [NoteTypeCode] = 257)
If that doesn't work, check the actual column names against what you are using, especially for case (upper/lower) and leading/trailing spaces. The best way to do that is to temporarily rename it, and look at the code for the "from name"

evaluating a textual equation into a number

AS a newbie to m I still can't get my head around it. Here is the query I have. It's gone through a number of steps to get to the below. How do I use Expression.Evaluate against the whole query against the column EntryFee, which for the sake of simplicity the query is called #"Nearly There". I want to Evaluate the entire column Entry Fee. To reiterate it needs to be done in Power Query "M"
Snapshot of table/query
You can either add a new custom column with the code
Expression.Evaluate([Entry Fee])
or do a column transformation
= Table.TransformColumns(#"Nearly There", {{"Entry Fee", Expression.Evaluate, type number}})
To generate this step, you can select the column and then Transform tab > Format > Trim and then replace Text.Trim with Expression.Evaluate in the generated code.

How to do multiple searches in an array formula?

I'm working on trying to get some formula working that'll vastly improve my QOL at work. I want the formula to run 2 searches for keywords that will be present in my "Title" column and my "SKU" column then return results based on a key that I have located in a second tab.
The above screenshot is my key. If products contain any of the keywords in the title column then the corresponding category is automatically assigned.
In the example screenshot below I have the formula working to do 1 search. It's searching the "Title" column (B) against the Keywords column in the Key and if the word exists within the title it gives a result back based on the row that the result was on.
What I want to achieve is to do multiple searches within the same column. I want to be able to first do the search by "Title", if no results are returned then I want it to search via the "SKU" column against the Keywords and Categories in the Key (Columns C & D)
In the below screenshot I've tried wrapping it in an IF statement
IF(TITLESEARCH=""),(SKUSEARCH),(TITLESEARCH)
The results I'm getting back seem to be working for the first part of the IF statement. It's giving me back results that are correct for the first part of the IF statement, but the second part doesn't seem to be working correctly and I can't figure out why. Where it should be assigning the category "Accessories" it's just giving me "FALSE" instead.
Here's a link to the spreadsheet if anyone would like to take a look. I've been wracking my brain on this for a few days now and I feel like I've come a long way.
https://docs.google.com/spreadsheets/d/1548QMP5qeAIFrbraGD_lkJkQWzKF4Td6FR1KkcOHHJM/edit?usp=sharing
I'm quite new to Google Sheets and excel formula (especially on this level) so please be gentle. It feels like the mistake I'm making might be glaringly obvious.
https://exceljet.net/formula/categorize-text-with-keywords
This is what my formula is based on.
=IF(ARRAYFORMULA(INDEX('Array Key'!B:B,MATCH(TRUE,ISNUMBER(SEARCH('Array Key'!A:A,B234)),0)))="",(ARRAYFORMULA(INDEX('Array Key'!D:D,MATCH(TRUE,ISNUMBER(SEARCH('Array Key'!C:C,A234)),0),(ARRAYFORMULA(INDEX('Array Key'!B:B,MATCH(TRUE,ISNUMBER(SEARCH('Array Key'!A:A,B234)),0))))))))
delete all your formulas and simply use:
=ARRAYFORMULA(IF(LEN(B2:B), IF(IFERROR(VLOOKUP(REGEXEXTRACT(A2:A,
TEXTJOIN("|", 1, 'Array Key'!C3:C)),
'Array Key'!C3:D, 2, 0))="",
IFERROR(VLOOKUP(REGEXEXTRACT(B2:B,
TEXTJOIN("|", 1, 'Array Key'!A3:A)),
'Array Key'!A3:B, 2, 0)),
IFERROR(VLOOKUP(REGEXEXTRACT(A2:A,
TEXTJOIN("|", 1, 'Array Key'!C3:C)),
'Array Key'!C3:D, 2, 0))), ))

RegEx in the select for DB2

I have a table with one column having a large json object in the format below. The column datatype is VARCHAR
column1
--------
{"key":"value",....}
I'm interested in the first value of the column data
in regex I can do it by .*?:(.*),.* with group(1) giving me the value
How can i use it in the select query
Don't do that, it's bad database design. Shred the keys and values to their own table as columns, or use the XML data type. XML would work fine because you can index the structure well, and you can use XPATH queries on the data. XPATH supports regexp natively.
You can use regular expression with xQuery, you just need to call the function matches from a SQL query or a FLORW query.
This is an example of how to use regular expressions from SQL:
db2 "with val as (
select t.text
from texts t
where xmlcast(xmlquery('fn:matches(\$TEXT,''^[A-Za-z 0-9]*$'')') as integer) = 0
)
select * from val"
For more information:
http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.xml.doc/doc/xqrfnmat.html
http://angocadb2.blogspot.fr/2014/04/regular-expressions-in-db2.html
DB2 doesn't have any built in regex functionality, unfortunately. I did find an article about how to add this with libraries:
http://www.ibm.com/developerworks/data/library/techarticle/0301stolze/0301stolze.html
Without regexes, this operation would be a mess. You could make a function that goes through the string character by character to find the first value. Or, if you will need to do more than this one operation, you could make a procedure that parses the json and throws it into a table of keys/values. Neither one sounds fun, though.
In DB2 for z/OS you will have to pass the variable to XMLQUERY with the PASSING option
db2 "with val as (
select t.text
from texts t
where xmlcast(xmlquery('fn:matches($TEXT,''^[A-Za-z 0-9]*$'')'
PASSING t.text as "TEXT") as integer) = 0
)
select * from val"