For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!
Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.
Hi,
Is there a function that converts char to bytes?
Thanks
Hello,
could you explain a bit more what you want? Are you for example looking for the function 'ASCII'?
SQL> select ascii( 'a' ) from dual;
ASCII('A')---------- 97
Best regards,
Bruno Vroman.
P.S. or the function DUMP? (More info in Oracle documentation).
Strings (char data) can also be converted to raw binary.
As Bruno said, you need to be specific about your problem, and supply the relevant details.
I have some DDL that provide the columns in char but I would like to convert these to bytes
e.g
VARCHAR2 (25 Char)
3026782 wrote:I have some DDL that provide the columns in char but I would like to convert these to bytese.g
3026782 wrote:
Why?
Well,
there is no rule (except in simple cases).
-a) it depends of the character set that you use. If you use a single byte character set, then 1 char occupies 1 byte (example: US7ASCII, WE8ISO8859P15) (this is what I cal "the simple cases"). But if you use a multibyte charset, it depends of the charset (AL32UTF8? UTF8?)
-b) with multibyte charsets: it depends of the characters themselves... "a" will (most probably) be coded on a single byte, "é" will be coded in 2 or 3 bytes...
Note that IMHO defining the VARCHAR( nnn ) as "nnn CHAR" makes more sense that as "nnn BYTE" (for example: you want to limit a name in your application to 30 characters, not 30 bytes). But note on the other hand that there is anyway a "harcoded" limit (like 4000 in usual situations) that is anyway expressed in BYTEs (so even if you define for example mystring varchar2(3000 char) and try to insert 3000 times "é", you will hit the 4000 byte limit.
Even with single byte charset I see an advantage at defining the VARCHAR2 with CHAR: if one day you have to expdp from this database and import in a database using a multibyte character set, you will be glad that the tables are created with CHAR (otherwise you might for example have something like: in source: mycol VARCHAR2( 3 BYTE ) with data 'éé', and when you expdp+impdp in target you receive an error message "value too long for column" because 3 BYTE is not enough (and 3 CHAR would be OK)
Do you want to convert VARCHAR2 size specification from a number of characters, to a number of bytes?
Or do you want to convert VARCHAR2 to BLOB or RAW instead?
And WHY?
If you enable us to understand the problem you are trying to solve, we are in a much better position to suggest what solutions can be considered.
LENGTH()
LENGTHB()
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions076.htm
... LENGTH(col) returns the number of CHARacters in the column.
... LENGTHB(col) returns the number of BYTES in the column.
do you want to convert your varchar2 data to binary data? like 101000111010 or something?
VARCHAR2(150 CHAR) means column can fit up to 150 characters. How many bytes it is depends on your character set. Oracle takes max number of bytes single character can occupy in database character set and multiplies it by 150. This way we are guaranteed column length will always accommodate 150 characters. You can check USER_TAB_COLUMNS for both character & byte length:
SQL> CREATE TABLE TBL(COL VARCHAR2(150 CHAR))
2 /
Table created.
SQL> SELECT DATA_LENGTH,
2 CHAR_COL_DECL_LENGTH,
3 CHAR_LENGTH,
4 CHAR_USED
5 FROM USER_TAB_COLUMNS
6 WHERE TABLE_NAME = 'TBL'
7 /
DATA_LENGTH CHAR_COL_DECL_LENGTH CHAR_LENGTH C
----------- -------------------- ----------- -
600 600 150 C
SQL>
SY.