Short version of the question: Is there a way of counting how many bytes will it take to store the characters of a string in a VARCHAR(n)
column in an Oracle database?
Longer version: The following Oracle SQL script will fail at the third statement. It will try to insert 10 characters in a VARCHAR(10)
column; however, one of these characters is the A with an acute accent.
CREATE TABLE TESTTABLE (NAME VARCHAR(10) NULL);
INSERT INTO TESTTABLE (NAME) VALUES ('abcdefghij');
--1 rows inserted.
INSERT INTO TESTTABLE (NAME) VALUES ('ábcdefghij');
--ORA-12899: value too large for column "ACME"."TESTTABLE"."NAME" (actual: 11, maximum: 10)
My C# application stores strings in an Oracle database and I can't just change the column type to NVARCHAR2(10)
, which would work. The application is expected to trim any larger string to a limit of 10 characters, so Oracle won't complain about its length. But a String.Length
-based trim is a very naif strategy: it would blindly leave "ábcdefghij" intact with all its 10 CLR characters.
How can I detect that 'á' will take 2 bytes in the database row so I can trim the string to "ábcdefghi" before issuing the INSERT
statement?
EDIT: this question is similar to Best way to shorten UTF8 string based on byte length