Extract from string before last instance of a character - BigQuery - sql

I am working on a problem where I would like to extract all parts of a string before the last instance of a repeating character.
I have the below data as an example:
string_variable
string_variable_two
My goal is to extract all text in the string before the last underscore "_".
So for the above I would end with:
string
string_variable
I cannot split because in some instances I have a single, and in some I have two. I also tried using a REGEX_EXTRACT but BigQuery will not allow look ahead functions for .*(?=_)

Consider below
select str,
ifnull(safe.left(str, instr(str, '_', -1) - 1), str)
from your_table
if applied to sample data in y our question - output is

Related

Check if a string has a combination of a substring and numbers in sql

how do I write a SQL where statement that checks if a string contains some substring and a number. For example:
string: macsea01
where string like 'macsea' plus a number
Regex is the most obvious solution to this question. Without more detail about the specific format of the string, I can suggest the following, which will match a sequence of a letter in the alphabet followed immediately by a digit:
where column_name like '%[a-zA-Z][0-9]%'
If you're literally looking for macsea at the beginning of the string followed by a digit, it would be:
where column_name like 'macsea[0-9]%'
Regex seem to bee a little slippery here, depending on your needs you can for instance divide the string into several parts, first the text part, and take the rest of the string, try to convert it into a number.
Somthing like this (but I think this perticular code is broken
where substring(column_name, 1, 6) = 'macsea' and cast(substring(column_name, 7, 1000) as int) > 0

Remove numbers from beginning of strings in a numbered list with SQLite

I have a long numbered list imported into a table, the strings are in the following format:
1. fdhsglahs sdhkgs
2. urgbvdgh ndovh
3. 8yhbnxjghr nvdfo dfhioj
...
9999. vnur neeu nu
I want to remove the numbers in the beginning of the string, the "." adjacent to the number, and any number of spaces that come immediately after the "." and before the next character (that is, before the beginning of the string itself).
Can't find a method to do that in SQLite.
Please notice, some of the strings contain numbers as part of the string, which are not to be removed.
For this requirement you can use string functions like substr(), instr() and ltrim():
select ltrim(substr(col, instr(col, '.') + 1))
from tablename
Replace col with the column's name.
this code returns the part of the string after the . left trimmed of spaces.
See the demo.
If you want to update the table:
update tablename
set col = ltrim(substr(col, instr(col, '.') + 1));
See the demo.

How to return string within quotes

I'm querying a dataset using Oracle SQL Developer and want to create a column with partial return data from a (CLOB) in another column.
The part I need is in quotes and i've used substring to extract however the value will change as will the length of the string. Is there a way to end when reach closing quotes instead of specifying length of string?
dbms_lob.substr(a.LINETESTRESULT,15,dbms_lob.instr(UPPER(a.LINETESTRESULT),'LHCRAG')+11) AS REFRESH_RAG
At the minute, 15 characters are returned, but the latest additions are shorter and showing the ", from the next entry. I need to replace the length attribute.
The result I get is:
Red_Session",
I need the output to be: Red_Session
or whatever the return value is, I just need it to end before the closing quote.
INSTR has a parameter nth (Occurrence number, starting at 1.). With this you can create something like this.
dbms_lob.substr(a.LINETESTRESULT,dbms_lob.instr(a.LINETESTRESULT, '"', 1, 2) - dbms_lob.instr(a.LINETESTRESULT, '"') - 1, dbms_lob.instr(a.LINETESTRESULT, '"') + 1) AS REFRESH_RAG
you might try REGEXP_SUBSTR like below. Alternatively you can find the position of the quote and use the substr...
SELECT
REGEXP_SUBSTR(a.LINETESTRESULT,
'"([^"]*)') AS REFRESH_RAG
FROM DUAL;
Managed to get this using the following code:
SELECT
dbms_lob.substr(UPPER(a.LINETESTRESULT), dbms_lob.instr(UPPER(a.LINETESTRESULT), '"',
dbms_lob.instr(UPPER(a.LINETESTRESULT), 'LHCRAG') + 11) -
(dbms_lob.instr(UPPER(a.LINETESTRESULT), 'LHCRAG') + 11),
dbms_lob.instr(UPPER(a.LINETESTRESULT), 'LHCRAG') + 11)
AS REFRESH_RAG

SQL / REGEX pattern matching

I want to use regex through sql to query some data to return values. The only valid values below returned would be "GB" and "LDN", or could also be "GB-LDN"
G-GB-LDN-TT-TEST
G-GB-LDNN-TT-TEST
G-GBS-LDN-TT-TEST
As it writes the first GB set needs to have 2 characters specifically, and the LDN needs to have 3 characters specifically. Both sets/groups seperated by an - symbol. I kind of need to extract the data but at the same time ensure it is within that pattern. I took a look at regex but I can't see how to, well it's like substring but I can't see it.
IF i undertsand correctly, you could still use of substring() function to extract the string parts separated by -.
select left(parsename(a.string, 3), 2) +'-'+ left(parsename(a.string, 2) ,3) from
(
select replace(substring(data, 1, len(data)-charindex('-', reverse(data))), '-', '.') [string] from <table>
) a
As in above you could also define the length of extracted string.
Result :
GB-LDN
GB-LDN
GB-LDN

T-SQL substring between delimiters in middle of string

I need to pull a substring from a string where there are multiple same delimiters. Has to be done in a sql query (no function calls, etc.)
Example of data:
DI:TIER1:VQ:SPI:H:SID005000009MTED0:FDCADIND:PS
PA:TIER4:VQ:SPI:H:SID005000009V3VO7:STPACPB3:CI
The data I need is between the last and second to last colons.
Data:
FDCADIND
STPACPB3
Data will always be in the same position. Last three characters will always be format of :XX
I started on a query but can't seem to get it to reverse correctly.
Select SubString('DI:TIER1:VQ:SPI:H:SID005000009MTED0:FDCADIND:PS', (CHARINDEX(':', 'DI:TIER1:VQ:SPI:H:SID005000009MTED0:FDCADIND:PS', 0) + 1),
(CharIndex(':', RIGHT('DI:TIER1:VQ:SPI:H:SID005000009MTED0:FDCADIND:PS', (LEN('DI:TIER1:VQ:SPI:H:SID005000009MTED0:FDCADIND:PS') - (CharIndex(':', 'DI:TIER1:VQ:SPI:H:SID005000009MTED0:FDCADIND:PS', 0)))), 0) - 1)) As NewString
Any help appreciated,
Chris
If the data (last two fields) are of fixed length:
SELECT LEFT(RIGHT('DI:TIER1:VQ:SPI:H:SID005000009MTED0:FDCADIND:PS',11),8)