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
Related
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.
I have a column as varchar2 datatype, the data in it is in format:
100323.3819823.222
100.323123.443422
1001010100.233888
LOL12333.DDD33.44
I need to remove the whole part after the first occurrence of '.'
In the end it should look like this:
100323
100
1001010100
LOL12333
I cant seem to find the exact substring expression due to the fact that there is not any fix length of the first part.
One way is to use REGEXP_SUBSTR:
SELECT REGEXP_SUBSTR(column_name,'^[^.]*') FROM table
The other way is to combine SUBSTR with INSTR, which is a bit faster, but will result in NULL if the data doesn't contain a dot, so you'll have to add a switch if needed:
SELECT SUBSTR(column_name, 1, INSTR(column_name,'.') - 1) FROM table
For oracle you can try this:
select substr (i,1,Instr(i,'.',i)-1) from Table name.
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
How to trim values which end with '?
Example: I have more than 100k rows with column report name like report[#name='Air Bookings by Status Code']
The actual report name is just
Air Bookings by Status Code
How do I trim this string?
I tried doing
substr(a.COGIPF_REPORTNAME, INSTR(a.COGIPF_REPORTNAME, '=', 1,1)+2, INSTR(a.COGIPF_REPORTNAME, ']')-2)
I'm able to trip the first part. How do I do the latter part?
You can use regexp_substr:
regexp_substr(a.COGIPF_REPORTNAME,q'<'([^']*)'>',1,1,null,1)
If you have a version wihtout regex, you can also use "pure" SQL functions:
SUBSTR(a.COGIPF_REPORTNAME,
INSTR(a.COGIPF_REPORTNAME, '=') + 2,
LENGTH(a.COGIPF_REPORTNAME) - INSTR(a.COGIPF_REPORTNAME, '=') - 3)
Note that the second parameter to SUBSTRING is the length of the desired substring.
This works if it is known, that the text always terminates with "']". Otherwise you will have to calculate the length differently:
SUBSTR(a.COGIPF_REPORTNAME,
INSTR(a.COGIPF_REPORTNAME, '=') + 2,
INSTR(a.COGIPF_REPORTNAME, ']') - INSTR(a.COGIPF_REPORTNAME, '=') - 3)
Why subtracting 3? Well - we have to subtract 2 for the two single quotes, but even if the text is empty, the position of the last single quote will be one more than that of the first single quote. Since an empty text has a length of 0, we must subtract one more.
See: http://sqlfiddle.com/#!4/32454a/10
using SQL 2008; I have the following string:
EMCo: 1 WorkOrder: 12770 WOItem: 10
I am trying to get the WorkOrder #.
When the string did not have the WOItem on end of it, I was able to use the following statement to get WorkOrder #.
[WorkOrder] = LTRIM(RTRIM(RIGHT(HQMA.KeyString,CHARINDEX(':',REVERSE(HQMA.KeyString))-1)))
This statement moves and may have double digits for the Co#, and it does not always have WOItem #. Was hoping to find something that would split after the ":" and just take 2nd group.
Any suggestions?
The patindex suggestion above will work beautifully, now if you still want to use your current statement, substring will pull the same values, and replace will take out WOItem. Not very elegant, it works whether you have WOItem or not:
select substring(LTRIM(RTRIM(RIGHT(REPLACE(HQMA.KeyString,'WOItem:',''),
CHARINDEX(':',REVERSE(REPLACE(HQMA.KeyString,'WOItem:','')))-1))),0,7)
select substring(LTRIM(RTRIM(RIGHT(REPLACE(HQMA.KeyString,'WOItem:',''),
CHARINDEX(':',REVERSE(REPLACE(HQMA.KeyString,'WOItem:','')))-1))),0,7)
How about using patindex()? Assuming the work order always has five characters:
select substring(HQMA.KeyString,
patindex('%WorkOrder: %', HQMA.KeyString) + 11,
5) as WorkOrder