Convert char to number if numeric or else null in Oracle - oracle

I would like to convert a character (I know it will always be char(1)) if the character is numeric ([0-9]) and return null if it is not numeric. IOW, I would like to catch the exception and return NULL for all non-convertible characters (those outside the 0-9 range). Is there a way to do that? I tried using TO_NUMBER but that fails if any of the rows have a non-numeric character.

You may use regexp:
select case when regexp_like(your_string, '^[[:digit:]]+$') then to_number(your_string) else NULL end
from (select '1d23' your_string from dual);
For char(1)
select case when ascii(c) between 48 and 57 then to_number(c) else null end
from (select '3' c from dual);
48 is ASCII code for '0' and 57 is ASCII code for '9', ascii function return ASCII code of the argument

Related

convert varchar2 value to decimal in oracle VIEW

i need to select a varchar2 value '>45%' (from table ABC, column name XYZ) as decimal, like select statement should return 0.45.
How to achieve this? i am not getting how to do this with regular_expression, or by Trimming and converting to number.
This should do it. Removes characters > and % and devide the result by 100. the character string is converted automatically to number
select regexp_replace('>45%', '\>|\%', '') / 100 from dual
Another approach: Remove all non-number characters:
select regexp_replace('>45%', '[^0-9]', '')/100 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;

what will translate function do if I want to change some chars to nothing?

I have a sql statement:
select translate('abcdefg', 'abc', '') from dual;
Why the result is nothing?
I think it should be 'defg'.
From the documentation:
You cannot use an empty string for to_string to remove all characters in from_string from the return value. Oracle Database interprets the empty string as null, and if this function has a null argument, then it returns null. To remove all characters in from_string, concatenate another character to the beginning of from_string and specify this character as the to_string. For example, TRANSLATE(expr, 'x0123456789', 'x') removes all digits from expr.
So you can do something like:
select translate('abcdefg', '#abc', '#') from dual;
TRANSLATE('ABCDEFG','#ABC','#')
-------------------------------
defg
... using any character that isn't going to be in your from_string.
select translate('abcdefg', 'abc', '') from dual;
To add to Alex's answer, you could use any character(allowed in SQL) for that matter to concatenate to remove all the characters. So, you could even use a space instead of empty string. An empty string in Oracle is considered as NULL value.
So, you could also do -
SQL> SELECT TRANSLATE('abcdefg', ' abc', ' ') FROM dual;
TRAN
----
defg
SQL>
Which is the same as -
SQL> SELECT TRANSLATE('abcdefg', chr(32)||'abc', chr(32)) FROM dual;
TRAN
----
defg
SQL>
Since the ascii value of space is 32.
It was just a demo, it is better to use any other character than space for better understanding and code readability.

What does caret(^) in Oracle translate function mean?

I encountered this statement from other developer's code which returns ABCDEF:
SELECT TRANSLATE('ABC123DEF456', '^0123456789', '^') FROM DUAL;
Then I tested with the following which have the same result:
SELECT TRANSLATE('ABC123DEF456', '^0123456789', ' ') FROM DUAL;
SELECT TRANSLATE('ABC123DEF456', '0123456789', ' ') FROM DUAL;
But this one returns null:
SELECT TRANSLATE('ABC123DEF456', '0123456789', '') FROM DUAL;
What does caret(^) mean? Why is it necessary?
TRANSLATE(expr, from_string, to_string):
You cannot use an empty string for to_string to remove all characters
in from_string from the return value. Oracle Database interprets the
empty string as null, and if this function has a null argument, then
it returns null.
Thus you cannot specify '' as the value for the to_string parameter, because it would be interpreted as null.
I suspect ^ is used here because it will never appear in the expr, and thus you will never see it in the resulting string as in TRANSLATE('ABC12^3DE0F456', '^0123456789', '^') which returns ABC^DEF.
Your original function SELECT TRANSLATE('ABC123DEF456', '^0123456789', '^') FROM DUAL; effectively strips all digits from the source string because for every matching digit in from_string there's no corresponding character in to_string, the other characters are just ignored.

Can you use ascii codes in SELECT statements? Specifically the SOH character

I have the following simple select statement :
select 'hello' || '|' || 'world' as MYVALUE from dual;
This gives me 2 words, delimited by a pipe.
I want to swap out the pipe delimeter for something else, such as the Ascii SOH character.
How can I use an ascii code in a select statement, such as \u01 ?
The CHR function returns the character for any ASCII code e.g. to get the character with ASCII code 1:
SELECT CHR(1) FROM DUAL;

Resources