ASCII value of strings - ascii

I am having trouble understanding ascii value conversion for a string that has more than one character.
For 'A', the ascii value is 65.
But for 'AA', it is 16705 and for 'ABC', it is 4276803,
How do we get to 'AA' and 'ABC' ascii values?

Related

RTRIM function trim extra character

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')

compare "string1" with comma separated "string2" without splitting

I need to match string1 with comma seperated values in string2 without splitting string2. For example, given:
String1: 'abc'
String2: '123,fgh,abc,tg,sd'
string1 should be compared with string2 and should return true as 'abc' is part of string2. Is there any way to do it??
Note. INSTR() doesn't solve this. If string2 is : '123,efabcde,34' it will return true but I need to compare only with comma separated values ("whole words")
Hope this may help you
INSTR2('123,fgh,abc,tg,sd.com', 'abc')
You could use REGEXP_LIKE function
Here is an example for your case:
with mydata as (
select '123,fgh,abc,tg,sd' d from dual
union
select '123,fgh,abcd,tg,sd' d from dual
union
select 'abc,123,fgh,abcd,tg,sd' d from dual
union
select 'abcd,xabc' d from dual
union
select 'abc' d from dual
)
select d,
case
when REGEXP_LIKE(d,',abc,|^abc,|,abc$|^abc$')
then 'Y'
else 'N'
end as res_y_n
from mydata
and the results:
123,fgh,abc,tg,sd Y
123,fgh,abcd,tg,sd N
abc Y
abc,123,fgh,abcd,tg,sd Y
abcd,xabc N
If str is the string containing the comma separated list of strings then
str is equal to abc
str is equal to abc,...
str is equal to ...,abc
str is equal to ...,abc,...
... is an arbitrary string.
You can use string functions, string operators or regular expression to check this. Using string operators you get
(str = 'abc') or
(str like 'abc,%') or
(str like '%,abc') or
(str like '%,abc,%')
if the string you search for (abc in this example) contains the characters % or _ then you have to quote them because they have a special meaning in LIKE expressions. You also have to quote the quote character. So if you search for 10% you use
(str = '10%') or
(str like '10\%,%') escape '\' or
(str like '%,10\%') escape '\' or
(str like '%,10\%,%') escape '\'
A regular expression solution with REGEXP_LIKE looks like more compact then a solution using LIKE or string functions but there are more special characters you have to quote.
If you use string functions (using INSTR, SUBSTR, LENGTH) then there are no special characters but the solution is less compact than this solution using LIKE.

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;

Convert char to number if numeric or else null in 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

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