How can I receive the name of the java exception class for example:
Say I have the following text captured in a db column:
`split_part('com.example.test.InvalidTradeTypeException', '.', 1 ) as exception_class`
I want the result of the split_part() to return InvalidTradeTypeException but I don't want to hard code the index position as the package structure can have many levels.
Of the top of my head I would do
reverse(split_part(reverse('com.example.test.InvalidTradeTypeException'),'.',1))
One method is to use regexp_replace():
select regexp_replace(col, '^.*[.]([^.]+)$', '\1')
Here is a db<>fiddle.
Related
I have a TEXT column where each text is formatted as such:
/customers/{customer_id}/views/{id1}~{id2}/
I am trying to fetch the id2 only.
My idea is how to split the string by the / character first, where I will have:
customers, {customer_id}, views, {id1}~{id2}.
And then, get the last position:
{id1}~{id2}. And then split it again by the ~ character, and finally get the last position.
The issue is that I am new to SQL and I have no idea if this is even possible. How can I do that and end up with only one column?
SELECT
split_part(thetext, '/', 4) as temp
// how do I proceed from here?
FROM mytable
EDIT:
Some examples:
/customers/1231341/views/1312391293~3432491/
/customers/2213441/views/424131~231321341/
The IDs are of different lengths as well.
Use regexp_replace() to capture the part you want while matching the whole input, and replacing (the whole input) with the capture:
select regexp_replace(thetext, '.*~(.*)/', '\1') as temp
from mytable
See live demo.
When i select a column i get the below results
{"ac92b9f2-c7c7-4d2a-982b-c06bb218a5ed"}
Is there a way to remove the {""} from the results?
This should work on most rdbms (though you should specify your rdbms anyway):
select substr(your_column, 3, length(your_column) - 4)
from your_table
Try this
Select
replace(replace(replace(col_name, '{', ''), '}', ''),
'"',") from table;
Or as per your data you can simply use
Select
Substr(col_name, 3,LENGTH(col_name)-4)
from table;
Check this for reference demo:
http://sqlfiddle.com/#!4/e24574/3
When you get the GUID value in your application from the database use this in your SQL query
SELECT CONVERT(VARCHAR(300), MYGUID)
instead of
SELECT MYGUID
Where MYGUID is the name of a field in your table.
This conversion CONVERT(VARCHAR(300), MYGUID) will remove bracket inside your application and you will get the value "ac92b9f2-c7c7-4d2a-982b-c06bb218a5ed" instead {"ac92b9f2-c7c7-4d2a-982b-c06bb218a5ed"}.
I have this code:
with demo as (
select 'WWW.HELLO.COM' web
union all
select 'hi.co.uk' web)
select regexp_matches(replace(lower(web),'www.',''),'([^\.]*)') from demo
And the table I get is:
regexp_matches
{hello}
{hi}
What I would like to do is:
with demo as (
select 'WWW.HELLO.COM' web
union all
select 'hi.co.uk' web)
select regexp_matches(replace(lower(web),'www.',''),'([^\.]*)')[1] from demo
Or even the big query version:
with demo as (
select 'WWW.HELLO.COM' web
union all
select 'hi.co.uk' web)
select regexp_matches(replace(lower(web),'www.',''),'([^\.]*)')[offset(1)] from demo
But neither works. Is this possible? If it isn't clear, the result I would like is:
match
hello
hi
Use split_part() instead. Simpler, faster. To get the first word, before the first separator .:
WITH demo(web) AS (
VALUES
('WWW.HELLO.COM')
, ('hi.co.uk')
)
SELECT split_part(replace(lower(web), 'www.', ''), '.', 1)
FROM demo;
db<>fiddle here
See:
Split comma separated column data into additional columns
regexp_matches() returns setof text[], i.e. 0-n rows of text arrays. (Because each regular expression can result in a set of multiple matching strings.)
In Postgres 10 or later, there is also the simpler variant regexp_match() that only returns the first match, i.e. text[]. Either way, the surrounding curly braces in your result are the text representation of the array literal.
You can take the first row and unnest the first element of the array, but since you neither want the set nor the array to begin with, use split_part() instead. Simpler, faster, and less versatile. But good enough for the purpose. And it returns exactly what you want to begin with: text.
I'm a little confused. Doesn't this do what you want?
with demo as (
select 'WWW.HELLO.COM' web
union all
select 'hi.co.uk' web
)
select (regexp_matches(replace(lower(web), 'www.',''), '([^\.]*)'))[1]
from demo
This is basically your query with extra parentheses so it does not generate a syntax error.
Here is a db<>fiddle illustrating that it returns what you want.
I have a table that looks like:
id
re|cid|13324242|
wa|cid|13435464|
fs|cid|2343532|
I want to extract information that is contained right after "|cid|" and before the following "|" element. That is:
13324242
13435464
2343532
I thought of substr() but there I don't know how to specify start and end element.
You could use REGEXP_REPLACE here (Standard SQL):
SELECT
id,
CASE WHEN id LIKE '%|cid|%'
THEN REGEXP_REPLACE(id, '^.*\|cid\|(\d+)\|.*$', '\1') END AS cid
FROM yourTable;
The idea is to use a regex replacement to extract the cid value from the id column, should it be present (and if not, we would just return NULL).
Here is a demo showing that the regex logic be correct.
If you want the third element (which appears to be the intention given the sample data), I would recommend split():
select (split(id, '|')[ordinal(3)]
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.