RTRIM function trim extra character - oracle

SELECT RTRIM('CO_CODE_VALUE', 'VALUE') FROM dual;
Result is CO_CODE_
But
SELECT RTRIM('CO_CODE_VALUE', '_VALUE') FROM dual;
Result is CO_COD
Does anybody know why?

From Oracle documentation:
RTRIM removes from the right end of char all of the characters that
appear in set.
That is, in your case, it starts from the right and removes all the characters in the set '_VALUE', not the string '_VALUE'.
For example:
select rtrim('EEEXEEEEEEE', '_VALUE') from dual
gives
EEEX
Because it starts from the right, and removes all the characters in ('V', 'A', 'L', 'U', 'E', 'S', '_'), that is all the occurences of 'E' going backward until it finds a character not in the set (the 'X')

Related

how to trim leading zero in oracle sql from concatenation text (text:number-number-number

how to trim leading zero in oracle sql from concatenation text
(text:number-number-number)
example(word:number-number-number) word can have text or double zero
but always has char before it after word, max digits separated by '-'
all time max 3 digits i want to keep zeros in first part. and after
that if remove leading 0 in sequence but keep it if it's only one 0
MachineAbc00:1-0-03 = MachineAbc00:1-0-3
MachineAbc00:1-001-02 = MachineAbc00:1-1-2
tried many combination, not successful , like
REGEXP_REPLACE ('MachineO00:1-0-03*', '0+(?!$)', '-')
REGEXP_REPLACE ('MTROPQFMO00:1-0-03*', '(-0){1,}', '-')
If all the input strings are in the exact format you said they are, then something like this should work:
with
sample_strings (str) as (
select 'MachineAbc00:1-0-03' from dual union all
select 'MachineAbc00:1-001-02' from dual union all
select 'MachineZzzyx:200-020-002' from dual union all
select 'machineCX032:0-000-0' from dual
)
select str as old_str,
regexp_replace(str, '([:-])0*(\d+)', '\1\2') as new_str
from sample_strings
;
OLD_STR NEW_STR
------------------------ ------------------------
MachineAbc00:1-0-03 MachineAbc00:1-0-3
MachineAbc00:1-001-02 MachineAbc00:1-1-2
MachineZzzyx:200-020-002 MachineZzzyx:200-20-2
machineCX032:0-000-0 machineCX032:0-0-0
The regular expression function finds any occurrence of (colon or dash) followed by (zero or more 0 characters/digits) followed by at least one more digit. The "zero or more 0 digits" is maximal with the property that there must be at least one more digit AFTER that match (even if that extra digit hapens to be a zero - see my last test string, which I added precisely in order to test that this works correctly). The function replaces each such occurrence with the first and third fragments, removing the middle one (the zeros you must remove from your string). The references \1 and \2 refer to the first and the second parenthesized sub-expressions - the punctuation mark (colon or dash) and, respectively, the final digits (excluding the leading zeros that must be removed).

FInd if the fifth position is a letter and not a number using ORACLE

How can I find if the fifth position is a letter and thus not a number using Oracle ?
My last try was using the following statement:
REGEXP_LIKE (table_column, '([abcdefghijklmnopqrstuvxyz])');
Perhaps you'd rather check whether 5th position contains a number (which means that it is not something else), i.e. do the opposite of what you're doing now.
Why? Because a "letter" isn't only ASCII; have a look at the 4th row in my example - it contains Croatian characters and these aren't between [a-z] (nor [A-Z]).
SQL> with test (col) as
2 (select 'abc_3def' from dual union all
3 select 'A435D887' from dual union all
4 select '!#$%&/()' from dual union all
5 select 'ASDĐŠŽĆČ' from dual
6 )
7 select col,
8 case when regexp_like(substr(col, 5, 1), '\d+') then 'number'
9 else 'not a number'
10 end result
11 from test;
COL RESULT
------------- ------------
abc_3def number
A435D887 not a number
!#$%&/() not a number
ASDĐŠŽĆČ not a number
SQL>
Anchor to the start of the string else you may get unexpected results. This works, but remove the caret (start of string anchor) and it returns 'TRUE'! Note it uses the case-insensitive flag of 'i'.
select 'TRUE'
from dual
where regexp_like('abcd4fg', '^.{4}[A-Z]', 'i');
Yet another way to do it:
regexp_like(table_column, '^....[[:alpha:]]')
Using the character class [[:alpha:]] will pick up all letters upper case, lower case, accented and etc. but will ignore numbers, punctuation and white space characters.
If what you care about is that the character is not a number, then use
not regexp_like(table_column, '^....[[:digit:]]')
or
not regexp_like(table_column, '^....\d')
Try:
REGEXP_LIKE (table_column, '^....[a-z]')
Or:
SUBSTR (table_column, 5, 1 ) BETWEEN 'a' AND 'z'

Replacing multiple CHR() from PLSQL string

I have a PLSQL string which contains chr() special characters like chr(10), chr(13). I want to replace these special characters from the string. I tried the following ways
select regexp_replace('Hello chr(10)Goodchr(13)Morning','CHR(10)|chr(13)','') from dual;
This do not works since regexp_replace do not replace chr() function. Then I tried
select translate('Hello chr(10)Goodchr(13)Morning', 'chr(10)'||'chr(13)', ' ') from dual;
It works partially, ie; I am forced to replace chr() with white space(third parameter). No option if I do not want to replace chr() with white spaces . If I pass third character as null then above query returns null result.
Anybody have any other methods?
The problem with your replacement isn't the logic, it's the syntax. Parentheses are regex metacharacters, and as such, if you want to replace a literal parenthesis, you need to escape it. So your pattern should be this:
chr\(13\)|chr\(10\)
Here is a working query:
select
regexp_replace('Hello chr(10)Goodchr(13)Morning','chr\(13\)|chr\(10\)','', 1, 0, 'i')
from dual
The fifth parameters in the above call to regexp_replace is 'i' and indicates that we want to do a case insensitive replacement.
Demo
The above logic removes the literal text chr(13) and chr(10) from your text. If instead you want to remove the actual control characters chr(13) and chr(10), then you may add those control characters to the alternation, e.g.
select
regexp_replace('Hello chr(10)Goodchr(13)Morning','chr\(13\)|chr\(10\)|chr(10)|chr(13)','', 1, 0, 'i')
from dual
Since regular expression functions are relatively expensive in Oracle I think it's worth showing the alternate method which just uses REPLACE for the same effect.
This replaces occurrences of the each control characters with a space;
SELECT REPLACE(REPLACE('ABC'||CHR(10)||'DEF'||CHR(13)||'GHI'||CHR(10)||'JKL',CHR(13),' '),CHR(10),' ') from dual
And this replaces occurrences of the strings 'CHR(13)' and 'CHR(10)';
select REPLACE(REPLACE('ABCCHR(10)DEFCHR(13)GHICHR(10)JKL','CHR(13)',' '),'CHR(10)',' ') from dual

Generating Random Values in pl/sql

I try to generate random value in pl/sql. But i need these values must be fix 12 character.
For example 654696544846, 234864687644, 438792168431
Thanks.
If you need a strictly numeric string made of decimal characters, you can use the dbms_random.value function which, when given 2 values X and Y, returns a random number between X (included) and Y (excluded)
select trunc(dbms_random.value(100000000000,1000000000000)) from dual
if you can accept also alphanumeric chars, you can use the dbms_random.string function:
select dbms_random.string('X',12) from dual
the second parameter is the required string length, the first dictates the subset of characters that are allowed in the string
'u', 'U' - uppercase alpha characters
'l', 'L' - lowercase alpha characters
'a', 'A' - mixed case alpha characters
'x', 'X' - uppercase alpha-numeric characters
'p', 'P' - any printable characters.
source: https://docs.oracle.com/database/121/ARPLS/d_random.htm
dbms_random.value returns a value between 0 and 1 with a precision of 39 digits, which you then multiply by 1000000000000 to get 12 digit number. It has a decimal part was well which can be removed with a suitable call to to_char which will also format it with a constant length.
select to_char(dbms_random.value * 1000000000000,'fm000000000000') from dual;

Replace a specific character in a string

How can I replace the 12th character of a string if it's equal to 'X'
For example if I have 'ABXD1X354XJXOKJX'; in this case I will replace the 'X' in the 12th position with 'Y'. result : 'ABXD1X354XJYOKJX'
I was thinking to use regexp_replace function to point the 12th character, test if it's equal to 'X', if yes replace it with 'Y' but it's more complicated than I thought
This should work for you using SUBSTR:
select
substr('ABXD1X354XJXOKJX', 1, 11) ||
case when substr('ABXD1X354XJXOKJX', 12, 1) = 'X' THEN 'Y' ELSE substr('ABXD1X354XJXOKJX', 12, 1) END ||
substr('ABXD1X354XJXOKJX', 13)
from dual
SQL Fiddle Demo
without knowing more of how are constituted your string, this should work:
-- matches:
select regexp_replace('12dasdf32432Xasdasd', '([[:alnum:]]{11})(X{1})', '\1Y' ) from dual
union
-- doesn't match:
select regexp_replace('12dasdf32432Zasdasd', '([[:alnum:]]{11})(X{1})', '\1Y' ) from dual
you can do:
CASE SUBSTR(yourfield, 12, 1) WHEN 'X' THEN 'Y' END
and add in concatenation as necessary
For this, it may be just as easy to use the SUBSTR function.
Example, if UPPER(SUBSTR('ABXD1X354XJYOKJX', 12, 1)) = 'X'

Resources