Could not remove unicode character - oracle

I have a text string in Oracle table ("NGUYỄN NGỌC HOÀNG"). I have tried to replace the Unicode string (my expected result is "NGUYEN NGOC HOANG") but it didn't work.
Then I tried to convert it to HEX as below: select rawtohex(UnicodeColumn) from TestTable;
Here is the result: 4E 47 55 59 C3 8A CC 83 4E 20 4E 47 4F CC A3 43 20 48 4F 41 CC 80 4E 47
Following the Unicode table: The corresponding characters are: NGUYỄN NGỌC HOÀNG
Could anyone help me this case (Oracle script), I don't have experience about the encoding.
Update 1:
I have tried the solution from Barbaros Özhan but it didn't work
SQL> with tab as ( select '&i_str1' str1 from dual ) 2 select str1,
utl_raw.cast_to_varchar2((nlssort(str1,'nls_sort=binary_ai'))) str2
from tab SQL> / Enter value for i_str1: NGUYỄN NGỌC HOÀNG

SQL> with tab as ( select '&i_str1' str1 from dual )
2 select str1, utl_raw.cast_to_varchar2((nlssort(str1,'nls_sort=binary_ai'))) str2 from tab
SQL> /
Enter value for i_str1: NGUYỄN NGỌC HOÀNG
and you get the desired result.

If you are using Java for connecting to Oracle DBMS, try the Java code below to convert to unaccent character, after that update the row in DBMS.
public static String unAccent(String s) {
String temp = Normalizer.normalize(s, Normalizer.Form.NFD);
Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
return pattern.matcher(temp).replaceAll("").replaceAll("Đ", "D").replace("đ", "");
}

You can try this function to convert to non-accent character:
CREATE OR REPLACE FUNCTION FN_CONVERT_TO_VN
(
STRINPUT IN NVARCHAR2
)
RETURN NVARCHAR2
IS
STRCONVERT NVARCHAR2(32527);
BEGIN
STRCONVERT := TRANSLATE(
STRINPUT, 'áàảãạăắằẳẵặâấầẩẫậđéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵÁÀẢÃẠĂẮẰẲẴẶÂẤẦẨẪẬĐÉÈẺẼẸÊẾỀỂỄỆÍÌỈĨỊÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢÚÙỦŨỤƯỨỪỬỮỰÝỲỶỸỴ', 'aaaaaaaaaaaaaaaaadeeeeeeeeeeeiiiiiooooooooooooooooouuuuuuuuuuuyyyyyAAAAAAAAAAAAAAAAADEEEEEEEEEEEIIIIIOOOOOOOOOOOOOOOOOUUUUUUUUUUUYYYYY'
);
RETURN STRCONVERT;
END;

Related

Oracle PL/SQL Code will not compile, ORA-06550: line 96, column 3:PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:

I have written the following PL/SQL code block and it uses a long array of 4 digit strings in the array.
The program block will loop over the array using the array(i) position as a query variable to return the the r_LDR_IORG record which is then printed with i, array(i), and r_LRD_IORG.PS_ORG_ID.
1 DECLARE
2 type array_t is varray(692) of varchar2(6);
3 array array_t := array_t('0118','0118','01R1','01R1','01R2','01R2','01R4','01R4',
4 '01R5','01R5','01R7','01R7','03BB','03BB','03BB','03BB',
5 '03R3','03R4','03R8','0684','0684','0684','0685','0685',
6 '0685','06CN','06CN','06CN','06GG','06GG','0799','0799',
7 '079T','079T','079T','079T','07R1','07R1','07R1','07R1',
8 '07R3','07R3','07R3','07R3','07R5','07R5','07R5','07R5',
9 '07R6','07R6','07R6','07R6','07R7','07R7','07R7','07R7',
10 '0954','0954','0954','0956','0956','0956','0959','0959',
11 '0959','09ZZ','09ZZ','09ZZ','14R1','14R1','14R1','14R2',
12 '14R2','14R2','16C4','16C4','16C4','16R3','16R3','16R5',
13 '16R5','16R5','16R5','16R7','16R7','16R7','16U4','16U4',
14 '16U4','16U4','1850','1850','1851','1852','1899','20R1',
15 '20R1','20R2','20R2','2181','2181','2181','2181','2182',
16 '2182','2182','2182','2184','2184','2184','2184','2186',
17 '2186','2186','2186','23C1','23C1','23C1','23C1','23C1',
18 '23C1','23C2','23C2','23C2','23C2','23C2','23C2','23FF',
19 '23FF','23FF','23FF','23R1','23R1','23R1','23R1','23R1',
20 '23R1','23R1','23R1','23R2','23R2','23R2','23R2','23R2',
21 '23R2','23R2','23R2','23R3','23R3','23R3','23R3','23R3',
22 '23R3','23R3','23R3','23R5','23R5','23R5','23R5','23R5',
23 '23R5','23R5','23R5','23R6','23R6','23R6','23R6','23R6',
24 '23R6','23R6','23R6','23R7','23R7','23R7','23R7','23R8',
25 '23R8','23R8','23R8','23R8','23R8','23R8','23VP','23VP',
26 '23VP','23VP','23X1','23X1','23X1','23X1','23X2','23X2',
27 '23X2','23X2','23X3','23X3','23X3','23X3','23X4','23X4',
28 '23X4','23X4','23XX','23XX','23XX','23XX','24MA','24MA',
29 '24MA','24MB','24MB','24MB','24MG','24MG','24MQ','24MQ',
30 '24MQ','24MR','24MR','24MR','26R2','26R2','26R4','26R4',
31 '299T','299T','299T','29R1','29R1','29R1','29R2','29R2',
32 '29R2','29R3','29R3','29R4','29R4','29R4','3071','3071',
33 '3071','3071','3072','3072','3072','3072','3075','3075',
34 '3075','3075','3083','3083','3083','3083','3084','3084',
35 '3084','3084','30XB','30XB','30XB','30XB','32M2','32M2',
36 '32M2','32M2','32M2','32M3','32M3','32M3','32M3','32M3',
37 '32M4','32M4','32M4','32M4','32M4','32M5','32M5','32M5',
38 '32M5','32M5','32M6','32M6','32M6','32M6','32M6','32M7',
39 '32M7','32M7','32M7','32M7','32M8','32M8','32M8','32M8',
40 '32M8','32MB','32MB','32MB','32MB','32MB','32MD','32MD',
41 '32MD','32MD','32MD','32ME','32ME','32ME','32ME','32ME',
42 '32MF','32MF','32MF','32MF','32MF','32MG','32MG','32MG',
43 '32MG','32MG','32MH','32MH','32MH','32MH','32MH','32MJ',
44 '32MJ','32MJ','32MJ','32MJ','32MK','32MK','32MK','32MK',
45 '32MK','32ML','32ML','32ML','32ML','32ML','32MM','32MM',
46 '32MM','32MM','32MM','32MN','32MN','32MN','32MN','32MN',
47 '32MP','32MP','32MP','32MP','32MP','32MV','32MV','32MV',
48 '32MV','32MV','32MW','32MW','32MW','32MW','32MW','32R2',
49 '32R2','32R2','32R2','32R2','32R3','32R3','32R3','32R3',
50 '32R3','32R4','32R4','32R4','32R4','32R4','32R5','32R5',
51 '32R5','32R5','32R5','32R6','32R6','32R6','32R6','32R6',
52 '32R7','32R7','32R7','32R7','32R7','32R8','32R8','32R8',
53 '32R8','32R8','32RA','32RA','32RA','32RA','32RA','32RB',
54 '32RB','32RB','32RB','32RB','32RC','32RC','32RC','32RC',
55 '32RC','32RD','32RD','32RD','32RD','32RD','32RE','32RE',
56 '32RE','32RE','32RE','32RF','32RF','32RF','32RF','32RF',
57 '32RG','32RG','32RG','32RG','32RG','32RH','32RH','32RH',
58 '32RH','32RH','32RJ','32RJ','32RJ','32RJ','32RJ','32RK',
59 '32RK','32RK','32RK','32RK','32RL','32RL','32RL','32RL',
60 '32RL','32RM','32RM','32RM','32RM','32RM','32RN','32RN',
61 '32RN','32RN','32RN','32RP','32RP','32RP','32RP','32RP',
62 '32RQ','32RQ','32RQ','32RQ','32RQ','32RW','32RW','32RW',
63 '32RW','32RW','341R','341R','341R','35R1','35R1','35R1',
64 '35R1','35R2','35R2','35R2','35R3','35R3','35R3','35R3',
65 '39Z1','4175','4175','4175','4175','4176','4176','4176',
66 '4176','4177','4177','4177','4177','4179','4179','4179',
67 '4179','41U1','41U1','41U1','41U1','41U2','41U2','41U2',
68 '41U2','41U3','41U3','41U3','41U3','41U4','41U4','41U4',
69 '41U4','41Z1','41Z1','41Z1','41Z1','4290','4290','4290',
70 '4290','4293','4293','4293','4293','42B7','42B7','42B7',
71 '42N7','42N7','42N7','42N7','4350','4350','4350','4351',
72 '4351','4351','4390','4390','4390','4390','4391','4391',
73 '4391','4391','43C9','43C9','43C9','43C9','44R1','44R1',
74 '44R1','44R1','46MM','46MM','46MM','46R1','46R1','46R1',
75 '46R2','46R2','46R2','46R2','489B','489B','48R1','48R1',
76 '48R3','48R3','5185','5185','5279','5279','5279','5297',
77 '5297','5297','521U','521U','521U','52R1','52R1','52R1',
78 '53R1','53R1','53R1','53R1','53R2','53R2','53R2','53R2',
79 '53R4','53R4','53R4','53R4','53R6','53R6','53R6','53R6',
80 '53R7','53R7','53R7','53R7','5480','5480','5481','5481',
81 '5482','5482','5501','5501','5501','5501','5501','5501',
82 '5501','5501','5501','5502','5502','5502','5502','5502',
83 '5502','5502','5502','5502','5502','5503','5503','5503',
84 '5503','5503','5503','5503','5503','5503','5503','55X6',
85 '55X6','55X6','55X6','560X','560X','560X','560X','561X',
86 '561X','561X','561X','56PP','56PP','56PP','56PP','62R2',
87 '62R2','65BT','65BT','65Z1','65Z1','65Z1','2R88','2R88',
88 '2S50','2S50','2S99','2S99','3E99','3E99','3E99','3E99',
89 '3EL1','3EL1','3EL1','3EL1'); -- Initialise it
90 BEGIN
91 for i in 1..array.count loop
92 for r_LRD_IORG IN (select * from LRDLicnse.LRD_IORG where IORG_TYP_DSC = 'BRANCH' and Record_status = 'A' and LGCY_IORG_ID = array(i)) loop
93 dbms_output.put_line(i, array(i), r_LRD_IORG.PS_ORG_ID);
94 end loop;
95 end loop;
96 END
97 /
After trying to compile the code block and run it I get the following error messages.
Error starting at line : 1 in command -
DECLARE
type array_t is varray(692) of varchar2(6);
array array_t := array_t('0118','0118','01R1','01R1','01R2','01R2','01R4','01R4',
'01R5','01R5','01R7','01R7','03BB','03BB','03BB','03BB',
'03R3','03R4','03R8','0684','0684','0684','0685','0685',
'0685','06CN','06CN','06CN','06GG','06GG','0799','0799',
'079T','079T','079T','079T','07R1','07R1','07R1','07R1',
'07R3','07R3','07R3','07R3','07R5','07R5','07R5','07R5',
'07R6','07R6','07R6','07R6','07R7','07R7','07R7','07R7',
'0954','0954','0954','0956','0956','0956','0959','0959',
'0959','09ZZ','09ZZ','09ZZ','14R1','14R1','14R1','14R2',
'14R2','14R2','16C4','16C4','16C4','16R3','16R3','16R5',
'16R5','16R5','16R5','16R7','16R7','16R7','16U4','16U4',
'16U4','16U4','1850','1850','1851','1852','1899','20R1',
'20R1','20R2','20R2','2181','2181','2181','2181','2182',
'2182','2182','2182','2184','2184','2184','2184','2186',
'2186','2186','2186','23C1','23C1','23C1','23C1','23C1',
'23C1','23C2','23C2','23C2','23C2','23C2','23C2','23FF',
'23FF','23FF','23FF','23R1','23R1','23R1','23R1','23R1',
'23R1','23R1','23R1','23R2','23R2','23R2','23R2','23R2',
'23R2','23R2','23R2','23R3','23R3','23R3','23R3','23R3',
'23R3','23R3','23R3','23R5','23R5','23R5','23R5','23R5',
'23R5','23R5','23R5','23R6','23R6','23R6','23R6','23R6',
'23R6','23R6','23R6','23R7','23R7','23R7','23R7','23R8',
'23R8','23R8','23R8','23R8','23R8','23R8','23VP','23VP',
'23VP','23VP','23X1','23X1','23X1','23X1','23X2','23X2',
'23X2','23X2','23X3','23X3','23X3','23X3','23X4','23X4',
'23X4','23X4','23XX','23XX','23XX','23XX','24MA','24MA',
'24MA','24MB','24MB','24MB','24MG','24MG','24MQ','24MQ',
'24MQ','24MR','24MR','24MR','26R2','26R2','26R4','26R4',
'299T','299T','299T','29R1','29R1','29R1','29R2','29R2',
'29R2','29R3','29R3','29R4','29R4','29R4','3071','3071',
'3071','3071','3072','3072','3072','3072','3075','3075',
'3075','3075','3083','3083','3083','3083','3084','3084',
'3084','3084','30XB','30XB','30XB','30XB','32M2','32M2',
'32M2','32M2','32M2','32M3','32M3','32M3','32M3','32M3',
'32M4','32M4','32M4','32M4','32M4','32M5','32M5','32M5',
'32M5','32M5','32M6','32M6','32M6','32M6','32M6','32M7',
'32M7','32M7','32M7','32M7','32M8','32M8','32M8','32M8',
'32M8','32MB','32MB','32MB','32MB','32MB','32MD','32MD',
'32MD','32MD','32MD','32ME','32ME','32ME','32ME','32ME',
'32MF','32MF','32MF','32MF','32MF','32MG','32MG','32MG',
'32MG','32MG','32MH','32MH','32MH','32MH','32MH','32MJ',
'32MJ','32MJ','32MJ','32MJ','32MK','32MK','32MK','32MK',
'32MK','32ML','32ML','32ML','32ML','32ML','32MM','32MM',
'32MM','32MM','32MM','32MN','32MN','32MN','32MN','32MN',
'32MP','32MP','32MP','32MP','32MP','32MV','32MV','32MV',
'32MV','32MV','32MW','32MW','32MW','32MW','32MW','32R2',
'32R2','32R2','32R2','32R2','32R3','32R3','32R3','32R3',
'32R3','32R4','32R4','32R4','32R4','32R4','32R5','32R5',
'32R5','32R5','32R5','32R6','32R6','32R6','32R6','32R6',
'32R7','32R7','32R7','32R7','32R7','32R8','32R8','32R8',
'32R8','32R8','32RA','32RA','32RA','32RA','32RA','32RB',
'32RB','32RB','32RB','32RB','32RC','32RC','32RC','32RC',
'32RC','32RD','32RD','32RD','32RD','32RD','32RE','32RE',
'32RE','32RE','32RE','32RF','32RF','32RF','32RF','32RF',
'32RG','32RG','32RG','32RG','32RG','32RH','32RH','32RH',
'32RH','32RH','32RJ','32RJ','32RJ','32RJ','32RJ','32RK',
'32RK','32RK','32RK','32RK','32RL','32RL','32RL','32RL',
'32RL','32RM','32RM','32RM','32RM','32RM','32RN','32RN',
'32RN','32RN','32RN','32RP','32RP','32RP','32RP','32RP',
'32RQ','32RQ','32RQ','32RQ','32RQ','32RW','32RW','32RW',
'32RW','32RW','341R','341R','341R','35R1','35R1','35R1',
'35R1','35R2','35R2','35R2','35R3','35R3','35R3','35R3',
'39Z1','4175','4175','4175','4175','4176','4176','4176',
'4176','4177','4177','4177','4177','4179','4179','4179',
'4179','41U1','41U1','41U1','41U1','41U2','41U2','41U2',
'41U2','41U3','41U3','41U3','41U3','41U4','41U4','41U4',
'41U4','41Z1','41Z1','41Z1','41Z1','4290','4290','4290',
'4290','4293','4293','4293','4293','42B7','42B7','42B7',
'42N7','42N7','42N7','42N7','4350','4350','4350','4351',
'4351','4351','4390','4390','4390','4390','4391','4391',
'4391','4391','43C9','43C9','43C9','43C9','44R1','44R1',
'44R1','44R1','46MM','46MM','46MM','46R1','46R1','46R1',
'46R2','46R2','46R2','46R2','489B','489B','48R1','48R1',
'48R3','48R3','5185','5185','5279','5279','5279','5297',
'5297','5297','521U','521U','521U','52R1','52R1','52R1',
'53R1','53R1','53R1','53R1','53R2','53R2','53R2','53R2',
'53R4','53R4','53R4','53R4','53R6','53R6','53R6','53R6',
'53R7','53R7','53R7','53R7','5480','5480','5481','5481',
'5482','5482','5501','5501','5501','5501','5501','5501',
'5501','5501','5501','5502','5502','5502','5502','5502',
'5502','5502','5502','5502','5502','5503','5503','5503',
'5503','5503','5503','5503','5503','5503','5503','55X6',
'55X6','55X6','55X6','560X','560X','560X','560X','561X',
'561X','561X','561X','56PP','56PP','56PP','56PP','62R2',
'62R2','65BT','65BT','65Z1','65Z1','65Z1','2R88','2R88',
'2S50','2S50','2S99','2S99','3E99','3E99','3E99','3E99',
'3EL1','3EL1','3EL1','3EL1'); -- Initialise it
BEGIN
for i in 1..array.count loop
for r_LRD_IORG IN (select * from LRDLicnse.LRD_IORG where IORG_TYP_DSC = 'BRANCH' and Record_status = 'A' and LGCY_IORG_ID = array(i)) loop
dbms_output.put_line(i, array(i), r_LRD_IORG.PS_ORG_ID);
end loop;
end loop;
END
Error report -
ORA-06550: line 97, column 0:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
; <an identifier> <a double-quoted delimited-identifier>
The symbol ";" was substituted for "end-of-file" to continue.
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
If you could help getting this pl/sql block up and running I would be much appreciative about it.
Thank you,
Steve Brooks
Obvious error, which causes this exception to be thrown: The END command, before the slash on a line by itself, must be ended in semicolon. Like this:
........
end loop;
END; -- notice the semicolon
/
After you fix this, you will run into another error. put_line takes only one argument, not three. If you want a string including those bits of data, and commas and spaces, you will have to build it using single-quotes for the parts that are hard-coded (comma, space) and the concatenation operator. The more common way would be something like this:
........
dbms_output.put_line('i = ' || i || ', array(i) = ' || array(i) ||
', PS_ORG_ID = ' || r.LRD_IORG.PS_ORG_ID);

PL/SQL How do i remove characters from data

I have rows that contain certain data such as:
EUR-40E
CP1-40E
CP1-36E
CP1FP-48E
My question is, how do i remove all characters leaving only the numbers after the hyphen.
I have tried to do it using this:
REGEXP_REPLACE (po.config_id, '\D')
But end up with this output:
140
140
136
It takes all characters but unfortunately the 1 before the hyphen remains?
Thanks
You could do it like that:
select substr(regexp_substr('EUR-40E','-[[:digit:]]+'),2) from dual;
Try :
select regexp_replace(substr(text,instr(text,'-'),length(text)), '[^0-9]', '') from dual;
replace text with the actual value e.g. CP1FP-48E56T
Example ( I created a dummy table foo with your values ) :
SQL> select * from foo;
TEXT
------------
EUR-40E
CP1-40E
CP1-36E
CP1FP-48E
CP1FP-48E54R
SQL> select regexp_replace(substr(text,instr(text,'-'),length(text)), '[^0-9]', '') output from foo;
OUTPUT
--------------------------------------------------------------------------------
40
40
36
48
4854

What is the exact NULL value for a field in Oracle?

Title says it all pretty much. What is the exact value that is assigned to a(n) a)Arithmetic b)String c)Logical field to represent NULL, in Oracle DBMS?
Thank you for your time!
Null is the absence of meaning, the absence of value. What gets assigned is null. Not even an ASCII null (ascii value 0) but nothing.
That's why there's a special operation to test for null . This will return false:
...
where col1 = null
We need to test for:
where col1 is null
"we were asked by a professor at uni to find what exactly that value is in these 3 respective cases"
Okay, let's investigate that. Here is a table with two rows:
SQL> create table t42 (
2 type varchar2(10)
3 , colv varchar2(10)
4 , coln number
5 , cold date
6 )
7 /
Table created.
SQL> insert into t42 values ('not null', 'X', 1, sysdate);
1 row created.
SQL> insert into t42 values ('all null', null, null, null);
1 row created.
SQL>Exp
Oracle has a function dump() which shows us the datatype and content of the passed value. Find out more.
What does dump() tell us about our two rows?
SQL> select type
2 , dump(colv) as colv
3 , dump(coln) as coln
4 , dump(cold) as cold
5 from t42;
TYPE COLV COLN COLD
---------- -------------------- -------------------- ----------------------------------
not null Typ=1 Len=1: 88 Typ=2 Len=2: 193,2 Typ=12 Len=7: 120,117,4,29,6,60,44
all null NULL NULL NULL
SQL>
So: the null columns have no data type, no value.
"I don't think dump is suitable for supporting any argument over what "exactly" gets stored to represent a null - because if the expression is null, it simply returns null by definition "
#JeffreyKemp makes a fair point. So let's dip a toe into the internals. The first step is to dump the data block(s);l the dump is written to a trace file:
SQL> conn / as sysdba
Connected.
USER is "SYS"
SQL> select dbms_rowid.rowid_relative_fno(t42.rowid) as fno
2 , dbms_rowid.rowid_block_number(t42.rowid) as blk
3 from a.t42
4 /
FNO BLK
-------- --------
11 132
11 132
SQL> alter system dump datafile 11 block 132;
System altered.
SQL> select value from v$diag_info where name = 'Default Trace File';
VALUE
--------------------------------------------------------------------------------
/home/oracle/app/oracle/diag/rdbms/orcl/orcl/trace/orcl_ora_3275.trc
SQL>
Because T42 is small it fits into only one block. Here is the interesting bit of the dump:
data_block_dump,data header at 0x805664
===============
tsiz: 0x1f98
hsiz: 0x16
pbl: 0x00805664
76543210
flag=--------
ntab=1
nrow=2
frre=-1
fsbo=0x16
fseo=0x1f73
avsp=0x1f5d
tosp=0x1f5d
0xe:pti[0] nrow=2 offs=0
0x12:pri[0] offs=0x1f7f
0x14:pri[1] offs=0x1f73
block_row_dump:
tab 0, row 0, #0x1f7f
tl: 25 fb: --H-FL-- lb: 0x1 cc: 4
col 0: [ 8] 6e 6f 74 20 6e 75 6c 6c
col 1: [ 1] 58
col 2: [ 2] c1 02
col 3: [ 7] 78 75 05 01 02 08 08
tab 0, row 1, #0x1f73
tl: 12 fb: --H-FL-- lb: 0x1 cc: 1
col 0: [ 8] 61 6c 6c 20 6e 75 6c 6c
end_of_block_dump
End dump data blocks tsn: 33 file#: 11 minblk 132 maxblk 132
We can see there are two rows in the table. The first row has entries for four columns; this is the 'not null' row. The second row has only one column: this is the 'all null' row. So, Jeffrey is quite right. All the trailing fields are null so Oracle stores nothing for them.
Answer from APC is fully right, let's give some information on "what does it mean":
Arithmetic: NULL basically means "not defined". Every math operation with NULL (i.e. "not defined") also returns NULL
String: NULL is an empty string, i.e. '' IS NULL returns TRUE - this behavior of Oracle is different to many others RDBMS.
Logical: I assume you mean what happens to BOOLEAN data types. Unlike almost any other programming language in PL/SQL a BOOLEAN variable can have three different states: TRUE, FALSE and NULL. Be aware of this special behavior when you work with BOOLEAN in PL/SQL.
In addition to #APC, in DB there is a something like 'ternary logic' in comparison operations, when we can say that value are equal, not equal and the third is "we don't know", cause of it value absence NULL, and even comparison of to NULL values gives the NULL, meaning that we have no information about operands

How to update a column of a table using a function in ORACLE

Being a novice in ORACLE and programming in general I have difficulty in updating series of cells in a column of a table in oracle.
The table:
ad soyad not1 not1 not3 BN HN
Ayşe Yılmaz 28 65 82
Melih Türkmen 45 27 56
Cemile Kara 54 65 99
Ragıp Pekkan 62 48 85
Jale Kale 48 75 52
Vehbi Kemal 18 65 63
Mehmet Hamarat 54 62 70
Murat Çokbilir 25 28 32
Harun Reşit 85 84 92
Cemal Yamak 95 23 90
My function :
create or replace
function basariNotu (a NUMBER, b NUMBER, c NUMBER) RETURN NUMBER IS
rbn NUMBER;
BEGIN
rbn:=(a*0.2)+(b*0.2)+(c*0.6);
RETURN rbn;
END basariNotu;
MY code that is supposed to update column 'bn' with rbn returned from the function:
DECLARE
d NUMBER;
e NUMBER;
f NUMBER;
rbn NUMBER;
j NUMBER;
BEGIN
j:=1;
LOOP
SELECT not1 INTO d FROM student;
SELECT not2 INTO e FROM student;
SELECT not3 INTO f FROM student;
basariNotu(d, e, f);
UPDATE student SET bn=rbn WHERE bn= NULL;
j:= j+1;
IF j>10 THEN
EXIT;
END IF;
END LOOP;
END;
You don't have any appropriate WHERE clauses in your queries, and your LOOP has no relationship to the rows being processed. The j counter merely makes the loop do the same action 9 times. Not only that, but your predicate bn= NULL will always evaluate to FALSE so it will never update any rows.
I think you probably want something like this:
FOR r IN (
SELECT student_id, not1, not2, not3
FROM student
WHERE bn IS NULL
) LOOP
rbn := basariNotu(a => r.not1, b => r.not2, c = r.not3);
UPDATE student SET bn=rbn WHERE student_id = r.student_id;
END LOOP;
The above code assumes your table has a unique identifier of some sort such as student_id. If it doesn't, you have more problems to deal with.
However, this is the slow-by-slow (row-by-row) style of programming, the habit of which you need to shake. The above could be done much more simply and efficiently with a single UPDATE statement, e.g.:
UPDATE student
SET bn = basariNotu(a => not1, b => not2, c = not3)
WHERE bn IS NULL;
For even better performance, you would avoid the function call completely, e.g.:
UPDATE student
SET bn = (not1*0.2)+(not2*0.2)+(not3*0.6)
WHERE bn IS NULL;

oracle raw type iteration

I am working with a table that contains a raw(200) field. From my client application I manage to get the value and store it in a byte[] and so that I can loop over it and get all the samples.
My raw data would be like ...
2C2B2E2B2D2C2933283030332B2F2D302F2B272F312E2B2F2F28242A2F322E
... and from there I would like to go from hex to decimal values and get an array such as 44,43,46,43
However, I would like to do a similar thing in a procedure but I don't know how to iterate over a raw field or how to cast it to byte array.
I tried with UTL_RAW.CAST_TO_BINARY_INTEGER but that would only give me the first sample
Given this data ...
SQL> select col1
2 from t23
3 /
COL1
--------------------------------------------------------------------------------
32433242324532423244324332393333323833303330333332423246324433303246324232373246
33313245324232463246323832343241324633323245
SQL>
... a SELECT like this will produce the requisite output...
SQL> select regexp_substr(utl_raw.cast_to_varchar2(col1), '([A-Z0-9]{2})', 1, level)
2 from t23
3 connect by level <= ceil(utl_raw.length(col1)/2)
4 /
REGEXP_SUBSTR(UTL_RAW.CAST_TO_VARCHAR2(COL1),'([A-Z0-9]{2})',1,LEVEL)
--------------------------------------------------------------------------------
2C
2B
2E
2B
...
2B
2F
2F
28
24
2A
2F
32
2E
31 rows selected.
SQL>
Use TO_NUMBER with the 'XX' mask to convert the hex into decimal ...
SQL> select to_number(
2 regexp_substr(utl_raw.cast_to_varchar2(col1), '([A-Z0-9]{2})', 1, level)
3 , 'XX')
4 from t23
5 connect by level <= ceil(utl_raw.length(col1)/2)
6 /
TO_NUMBER(REGEXP_SUBSTR(UTL_RAW.CAST_TO_VARCHAR2(COL1),'([A-Z0-9]{2})',1,LEVEL),
--------------------------------------------------------------------------------
44
43
46
43
45
44
41
...
Finally, to populate an array, and populate it in PL/SQL with the bulk collection syntax:
create type int_nt as table of integer
/
declare
ints int_nt;
begin
select to_number(
regexp_substr(utl_raw.cast_to_varchar2(col1), '([A-Z0-9]{2})', 1, level)
, 'XX')
bulk collect into ints
from t23
connect by level <= ceil(utl_raw.length(col1)/2);
end;
/
Probably there's a more efficient way to solve this but I managed to get my result by using utl_raw.length and utl_raw.substr over my raw data and iterating with an standard plsql loop and converting each substring to decimal with utl_raw.cast_to_binary_integer

Resources