translate command in oracle - sql

I wanted when the below script do working, deleted special characters. But for first character in second parameter, this action is not working.
select translate('A##!a123','!##$%^&*)(_+',' ') from dual;
to replace ! with space, but did not remove from string.

One way is change your function to REGEXP_REPLACE
SELECT REGEXP_REPLACE('A##!a1+23', '[!##$%^&*)(_+]', '')
FROM dual;

Related

Oracle - How to use a function inside an escape with q

I know that I can escape strings in a statement like this:
select 'That''s a really funny ''joke''' from dual; --returns: That's a really funny 'joke'
Or like this:
select q'[That's a really funny 'joke']' from dual; --returns: That's a really funny 'joke'
Both are working fine.
Let's say I need to escape a string exactly in that select statement and I also need to use a function.
select q'[myfunction(somestringvariable)]' from dual;
Of course this only returns "myfunction(somestringvariable)"
As said, I need to escape the results of the function (can't be done inside the function, escape needs to happen in this select statement).
Can I use the function somehow inside a string escaped with "q"?
Thanks!
To double single quotes in the function result:
REPLACE(myfunction(somestringvariable), '''', '''''')
Oracle does not support template literals.
Just use:
SELECT myfunction(somestringvariable) FROM DUAL;
or, if the function does not return a string:
SELECT TO_CHAR(myfunction(somestringvariable)) FROM DUAL;
If you want to concatenate a quoted string literal and a function result then use string concatenation.
SELECT q'[That's a really funny 'joke']'
|| myfunction(somestringvariable)
FROM DUAL;

Postgresql: Extracting substring after first instance of delimiter

I'm trying to extract everything after the first instance of a delimiter.
For example:
01443-30413 -> 30413
1221-935-5801 -> 935-5801
I have tried the following queries:
select regexp_replace(car_id, E'-.*', '') from schema.table_name;
select reverse(split_part(reverse(car_id), '-', 1)) from schema.table_name;
However both of them return:
01443-30413 -> 30413
1221-935-5801 -> 5801
So it's not working if delimiter appears multiple times.
I'm using Postgresql 11. I come from a MySQL background where you can do:
select SUBSTRING(car_id FROM (LOCATE('-',car_id)+1)) from table_name
Why not just do the PG equivalent of your MySQL approach and substring it?
SELECT SUBSTRING('abcdef-ghi' FROM POSITION('-' in 'abcdef-ghi') + 1)
If you don't like the "from" and "in" way of writing arguments, PG also has "normal" comma separated functions:
SELECT SUBSTR('abcdef-ghi', STRPOS('abcdef-ghi', '-') + 1)
I think that regexp_replace is appropriate, but using the correct pattern:
select regexp_replace('1221-935-5801', E'^[^-]+-', '');
935-5801
The regex pattern ^[^-]+- matches, from the start of the string, one or more non dash characters, ending with a dash. It then replaces with empty string, effectively removing this content.
Note that this approach also works if the input has no dashes at all, in which case it would just return the original input.
Use this regexp pattern :
select regexp_replace('1221-935-5801', E'^[^-]+-', '') from schema.table_name
Regexp explanation :
^ is the beginning of the string
[^-]+ means at least one character different than -
...until the - character is met
I tried it in a conventional way in general what we do (found
something similar to instr as strpos in postgrsql .) Can try the below
SELECT
SUBSTR(car_id,strpos(car_id,'-')+1,
length(car_id) ) from table ;

replace two characters in one cell

I am using this query to replace one character in a cell
select replace(id,',','')id from table
But I want to replace two characters in a cell.
If the cell is having this data (1,3.1), and I want it to look like this (131).
How can I replace two different characters in one cell?
Use TRANSLATE instead of REPLACE(). It replaces each occurrence of a character in the first pattern with its matched character in the second. To remove characters, simply leave cut short the replacement string:
select translate(id, '1,.', '1') id from table
Note that the second string cannot be null. Hence the need to include 1 (or some other character) in both strings.
Find out more.
Obviously the more characters you need to convert/remove the more attractive TRANSLATE() becomes. The main use for REPLACE is changing patterns (such as words) rather than individual characters.
Can use
select replace(translate(id,',.',' '),' ','') from table;
or
select regexp_replace('1,3.1','[,.]','') from dual;
or
select replace(replace(id,',',''),'.','') from table;
Call the replace again.
select replace(replace(id,',',''), '.','') id from table
Do this:
select REPLACE(REPLACE(id,',',''),'.','')
Or use a regular expression:
select regexp_replace(id, '[.,]', '') id from table
Find out more

Oracle: How to use regexp_substr in this case

I have a table in Oracle where one of the column contains UserIds which are in the form of \. For eg "fin\george", "sales\andy" etc. How can I use REGEXP_SUBSTR function to get only the from the UserIds. ie I want to fetch only "george", "andy" etc. I have achieved the desired reult using SUBSTR function but I want to use REGEXP_SUBSTR in this case.
I tried doing this:
SELECT REGEXP_SUBSTR('fin\george','\[^\]+,') "UserName" FROM DUAL;
but it did'nt help. Can anyone please point out my mistake ?
I believe you want to use a regexp_replace with a backreference. I'm assuming that all the characters before and after the \ are alphabetic. If you allow numbers, you'd want to use the [[:alnum:]] rather than [[:alpha::].
1* SELECT REGEXP_replace('fin\george',
'([[:alpha:]]+\\)([[:alpha:]]+)$',
'\2') "UserName"
FROM DUAL
SQL> /
UserNa
------
george
SQL> SELECT REGEXP_SUBSTR('fin\george', '[^\]+', 1, 2) AS userId from dual;
USERID
------
george
See this Oracle Base article
select regexp_replace( 'fin\george', '.*\\', null ) from dual;
returns george.
The regex will match any character followed by the \ (which is escaped), as many times as possible (greedy).
So it will match everything up to the final \.
Then the matching string is replaced with null.
null is the default so
select regexp_replace('fin\george', '.*\\' ) from dual;
does the same thing
Same expression can extract filename from the end of pathname e.g.
select regexp_replace ('fin\fin2\fin3\fin4\george', '.*\\' ) from dual;
will also return george.
You have to use escaping: \\ instead of \
The easiest way (IMHO) to do this is the following:
SELECT REGEXP_SUBSTR('fin\george', '[^\\]+$') AS "UserName" FROM DUAL;
The issues with your original query were (a) that the \ character was not escaped and (b) there was an extraneous comma in the regular expression. I've used the end-of-string anchor $ here, assuming that there are not more than two elements delimited by \. If there are more than two, and you need only the second one, you can use the following:
SELECT REGEXP_SUBSTR('fin\george\ringo', '[^\\]+', 1, 2) AS "UserName"
FROM DUAL;
This tells Oracle to start looking at the first character of the string and return the second match.

Trim the ' (apostrophe) character in an sql statement

I'm using the DBMS_SQL package that returns the value '12345' as a value for a column name.
How do you trim the apostrophes out from the value so that it could be converted into a number?
Something like the following should trim the apostrophes:
substr(columnName, 2, length(columnName) - 2)
(As an aside, it's a pretty odd DB with a column name that's an integer, isn't it?)
This will remove leading and trailing apostrophes from a value:
RTRIM(LTRIM(value,''''),'''')
or if you prefer:
RTRIM(LTRIM(value,chr(39)),chr(39))
you could just use the "REPLACE" function to replace all single-quotes in the string with NULLs.
eg .
with quotes :
select 'Hello,''World''' from dual
quotes removed:
select replace('Hello,''World''','''',NULL) from dual
But, a columnname in Oracle cannot begin with a digit, so 12345 is invalid as a column-name anyway.