I have a requirement to manipulate a string to get the required value.
I need to change ACTUALSTRING99 to ACTUALSTRING_99. Currently I am passing this ACTUALSTRING99 to a function and returning it as ACTUALSTRING_99 in the following way.
SELECT 'VALUE' AS ACTUAL,
REGEXP_REPLACE('VALUE', '[[:digit:]]') AS STRING,
REGEXP_REPLACE('VALUE', '[[:alpha:]]') AS DIGIT,
concat(concat(REGEXP_REPLACE('VALUE', '[[:digit:]]'),'_'),REGEXP_REPLACE('VALUE', '[[:alpha:]]')) AS REQUIRED
FROM dual;
Passing VALUE asACTUALSTRING99. Do we have any other simple way (using or without using regular expression) to do it with out calling the function?
To prepend underscore before numerical part of the string, you can simply use regexp_replace with backreference.
SELECT
REGEXP_REPLACE('actualstring99','([[:digit:]]+)','_\1')
FROM dual;
You can do this using substring and length functions:
SELECT SUBSTR('VALUE',0,LENGTH('VALUE')-2) || '_' ||
SUBSTR('VALUE',LENGTH('VALUE')-2,LENGTH('VALUE'))
FROM dual;
Related
I need to convert string to decimals.
So the string value is: 89,333,22.2345
So i want to keep all decimal places and convert it to: 8933322.2345.
I tried the following query:
select to_number(replace(nvl(89,333,22.2345),0),',','') from dual;
This rounds it to 893322. But i want result with all decimals:
If i try running this query:
select to_number((replace(nvl(89,333,22.2345),0),',',''),'9999.99') from dual;
it throws error.
Try this:
select to_number('89,333,22.2345','99,999,99.9999') from dual;
I think you're passing the number as a parameter. Otherwise why would you use nvl. Then I see the query you need is going to be similar like that
select to_number(replace(nvl('89,333,22.2345','0'),',','')) from dual;
Things were wrong at your code:
since number is a varchar, it has to be placed between apostrophes
nvl takes parameters divided by a comma. And there was one closing bracket too much
nvl(89,333,22.2345),0) -> nvl('89,333,22.2345',0)
In this code I am calling this FSG.REPLACE_STRING function which has 2 parameters, original string and special characters string. The original string is a select query from a table and special character string is 'A'.
I have written the code:
FSG.REPLACE_STRING ( (SELECT CAST(NVL(PRAD_ID , ' ') AS CHAR(12))
FROM FSG_WRK.FSG_PRCB_AUXDB_PRAD WHERE PRAD_ID= '003204091007'), A );
but this is not working.
You are trying to pass the table column value into the function, so you need to restructure your statement:
SELECT FSG.REPLACE_STRING (CAST(NVL(PRAD_ID, ' ') AS CHAR(12)), 'A')
FROM FSG_WRK.FSG_PRCB_AUXDB_PRAD
WHERE PRAD_ID= '003204091007';
Although the NVL() part seems a bit pointless if you're filtering for a specific (not-null) value in the query. Casting to char looks suspicious too.
I would like to get:
82961_01B04WZXQQSUGJ4YMRRT2A7TRHK_MR_2_1of1
from the following expression
LASTNAME_FIRSTNAME_82961_01B04WZXQQSUGJ4YMRRT2A7TRHK_MR_2_1of1
Does someone know how I can get this using regexp_substr ?
EDIT
Basically I have a field which has 7 sets each separated by _ . The string I gave is just one example. I wanted to retrieve everything after the second _ . There is no fixed character length so I can not use a substr function. Hence I was using regexp_substr. I was able to get away by using a simplified version
Select FILE_NAME, ( (REGEXP_SUBSTR(FILE_NAME,'[^_]+_',1,3)) ||
(REGEXP_SUBSTR(FILE_NAME,'[^_]+_',1,4)) ||
(REGEXP_SUBSTR(FILE_NAME,'[^_]+_',1,5)) ||
(REGEXP_SUBSTR(FILE_NAME,'[^_]+_',1,6)) ||
(REGEXP_SUBSTR(FILE_NAME,'[^_]+',1,7)) ) as RegExp
from tbl
Here is some more data from the FILE_NAME field
LAST_FIRST_82961_01B04WZXQQSUGJ4YMRRT2A7TRHK_MR_2_1of1
SMITH_JOHN_82961_0130BPQX9QZN9G4P5RDTPA9HR4R_MR_1_1of1
LASTNAME_FIRSTNAME_99999_01V0MU4XUQK0Y24Y9RYTFA7W1CM_MR_3_1of1
To get everything after the second underscore, you do not need regular expressions, but can use something like the following:
select substr(FILE_NAME, instr(FILE_NAME, '_', 1, 2) +1 ) from tbl
The instr returns the position of the second occurrence of '_', starting by the first character; the substr simply gets everything starting from the position given by instr + 1
From your Requirement, you can just go ahead and use the simple SUBSTRfunction. Its faster, and it addresses the simple need to remove the String LASTNAME_FIRSTNAME.
select substr('LASTNAME_FIRSTNAME_82961_01B04WZXQQSUGJ4YMRRT2A7TRHK_MR_2_1of1', 20) data_string
from dual;
Output:
data_string
-----------------
82961_01B04WZXQQSUGJ4YMRRT2A7TRHK_MR_2_1of1
Unless you have another underlying logic you need to address?
Kindly clarify so i can edit the answer accordingly.
I have a string IN-123456; now I need to trim the IN- from that string. I tried as in Oracle
select trim('IN-' from 'IN-123456) from dual;
but I get an error
ORA-30001: trim set should have only one character
30001. 00000 - "trim set should have only one character"
*Cause: Trim set contains more or less than 1 character. This is not
allowed in TRIM function.
How can I solve this?
A simple replace wouldn't do the trick?
select replace('IN-123456', 'IN-', '') from dual;
Thanks for the result...
It can be solved with LTRIM() function
Clearly, TRIM is not the correct function for the job. You need to REPLACE the (sub)string IN- with nothing:
SELECT REPLACE('IN-123456', 'IN-') FROM dual;
Be aware that this will replace all occurrences of IN- anywhere in the string. If that's not appropriate, but the IN- will always be at the start of the string, then you could use SUBSTR instead:
SELECT SUBSTR('IN-123456', 4) FROM dual;
you just forget to complete single quote
select trim('IN-' from 'IN-123456') from dual;
now try this
Trim Function is always remove one char from string
Here is the example -
SELECT TRIM(both 'P' FROM 'PHELLO WORLDP') FROM DUAL
Out put -HELLO WORLD
You may use LEADING /TRAILING insert of Both.
In your case "IN-" holding three char.
Using the Oracle to_char(number) function, is it possible to append ascii characters to the returned string?
Specifically, I need to add a percentage character to the returned string.
"select to_char(89.2244, '999G999G999G999G990D00') from dual" -->
returns "89.22". I need a format pattern that returns "89.22%".
I am using this through reports in Application Express, so cannot simply concatenate "%" to the query, i need to put it in the number format.
So you can't wrap the to_char with a CONCAT?
select concat(to_char(89.2244, '999G999G999G999G990D00'),'%') from dual
You can't do it right in the number format.
If you are able to change NLS_CURRENCY for you session, you can do the following:
SELECT TO_CHAR(1.2, '999G999G999G999G990D00L' /*, 'NLS_CURRENCY=%' */)
FROM dual
---
1,20%
Quick and dirty way:
select to_char(89.2244, '999G999G999G999G990D00L', 'NLS_CURRENCY=''%''') from dual;
SYS # orant11g >select to_char(89.2244, '999G999G999G999G990D00')||'%' from dual;
TO_CHAR(89.2244,'999G999
------------------------
89.22%
Just use the || bars instead of the concat function.