Oracle SQL possible to trim characters before a character is found? - sql

Not sure if it is possible to do with trim in a select statement:
Basically want to trim anything characters in a string BEFORE a character is found:
say there are values (names) Oliver, Dave and I want to trim all characters before 'v' is found so those values would be 'ver' and 've' afterwards
Again it kind of sounds too complicated for a trim statement unless there is an easy way to combine it with a LIKE to search for such a thing.
May be getting ahead of myself here but would be a useful thing if anyone can help enlighten it :)

Maybe something like
SELECT REGEXP_SUBSTR('David', 'v.*') from dual
WHERE REGEXP_LIKE('David', 'v.*')
Description about REGEXP_SUBSTR: http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions131.htm
REGEXP_LIKE: http://docs.oracle.com/cd/B14117_01/server.101/b10759/conditions018.htm

You could do it with REGEXP, or with SUBSTR/INSTR:
SELECT CASE WHEN INSTR(mycolumn,'v') > 0
THEN SUBSTR(mycolumn,INSTR(mycolumn,'v'))
ELSE mycolumn
END
FROM ....

Related

Redshift SQL REGEXP_REPLACE function

I have a value which is duplicated from source (can't do anything about that). I have read some examples here https://docs.aws.amazon.com/redshift/latest/dg/REGEXP_REPLACE.html
Example value:
ABC$ABC$
So just trimming anything after the first '€'. I tried this, but I cannot figure out the correct REGEX expression.
REGEXP_REPLACE(value, '€.*\\.$', '')
So just trimming anything after the first '€'.
Why use regex at all? Why not just..
SELECT LEFT(value, CHARINDEX('€', value)-1)
If not all your data has a euro sign, consider WHERE value like '%€%'
Your current regex pattern is including a dot as the final character. Remove it and your approach should work:
SELECT REGEXP_REPLACE(value, '€.*$', '') AS value_out
FROM yourTable;
Or you can take the initial sequence of non-€ characters:
REGEXP_SUBSTR(value, '^[^€]+')

string between two special characters impala sql

Hi all I am trying to write sql for selecting string between two special characters.
example: in the table, field value like 7185878969-129981041-000000 . how can I select only middle portion 129981041 without hard coding. What will be the best way to go about this?.Please provide sample code. Thanks
Impala has split_part():
select split_part(col, '-', 2)
Try this for MySQL:
SELECT REVERSE(SUBSTRING_INDEX(REVERSE(SUBSTRING_INDEX(Column,'-',2)),'-',1))
FROM table_name;
Result:
129981041

Query to remove character and text from field

I am working with a PostgreSQL database. I have a column that contains rows that look like this:
I am to replace the 'PC' text with '00000' and then remove the period and all text after.
So for example, row 5 should look like 0000055000 after the transformation.
I was able to the 'PC' with the overlay function. So my current query looks like this:
select set_name, overlay(set_name placing '00000' from 1 for 2 ) FROM
src.sap_setnode
WHERE set_name LIKE'PC%'
From there how can I remove the period and everything after it?
Thanks
I actually found the answer - if anyone has a similar question. The split_part function can be used to split the field by using '.' as a delimiter and then grabbing the first part.
SELECT
split_part( overlay(set_name placing '00000' from 1 for 2 ),'.',1) FROM
src.sap_setnode
SELECT SPLIT_PART(REPLACE(set_name ,SUBSTR(set_name ,1,2),'00000'),'.',1) FROM t WHERE set_name LIKE 'PC%' OR set_name NOT LIKE 'PC-%' OR set_name NOT LIKE 'PCA%'
Try to use regular expression, for example:
UPDATE TABLE SET set_name = regexp_replace(set_name, '^PC(.+)\..+$', '00000\1');
I'm not entirely sure that this covers all the corner cases you might encounter, so you might need to tweak the regex a bit :)
Also documentation for regex_replace:
http://www.postgresql.org/docs/9.1/static/functions-string.html
using regexp_replace:
select regexp_replace (regexp_replace(set_name,'^PC','00000') , '\.(.*)', '');

How can I use RTRIM or REPLACE when I know the length I want to trim but not what it may contain?

I need to RTRIM the last 7 characters from a result set in an Oracle query. These 7 chars can be anything; spaces, alpha numeric etc... and I don't know the exact length of any value.
So for example I'd like to run something like this
SELECT RTRIM (COl_A, (SELECT LENGTH (COL_A)-7) FROM TABLE_ONE;
or a replace equivalent
SELECT REPLACE(COL_A, (SELECT LENGTH (COL_A)-7 FROM TABLE_ONE),'');
Do I need to do something with SUBSTRING maybe?
I know how to remove/replace specific chars but I'm having trouble when dealing with unknown chars. I've seen a few examples of similar problems but they seem unnecessarily complicated... or does this require a more in depth solution than I think it should?
As always thanks in advance for advice or hints.
You are in search of the substr function.
select substr(col_a, 1, length(col_a) - 7) from table_one
Actually, the correct solution is:
select substr(col_a, 1, (case when length(col_a) < 7 then 0 else length(col_a) - 7 end) from table_one
To be general, you would want to take into account what happens when the length is less than 7.

How to parse out quoted values from a column with sql

I have a field that has values like this...
s:10:"03/16/1983";
s:4:"Male";
s:2:"No";
I'd like to parse out the quoted values.
its going to be some sort of combination of substr and instr
its the doublequote i have issues finding its position.
i have tried things like select substr(field_value, instr(field_value,'"'),instr(field_value,'"',null,2)) from table where etc
apologies a noob question...
Here's something that should work (unable to test at the moment):
select substr(substr(field_value, instr(field_value,':')+1, CHAR_LENGTH(field_value)-1),
instr(substr(field_value, instr(field_value,':')+1, CHAR_LENGTH(field_value)-1),':')+1)
Edit: Putting my comment in the answer:
select substr(field_value, instr(field_value,'\"'),CHAR_LENGTH(field_value)-1)