3 Replies Latest reply: Apr 1, 2008 8:58 AM by ddevienne RSS

    Difference between VARCHAR & NVARCHAR using data type code

    472553
      I am tryin to retrieve metadata from Oracle using OCI in my code written in C#. It has been specified that VARCHAR & NVARCHAR has same data type code, both having 1 (same goes for CHAR & NCHAR). How to distinguish between the two when we can not have different codes or is there any way to have different code values?

      Thanks & Regards
      Mohit
        • 1. Re: Difference between VARCHAR & NVARCHAR using data type code
          631265
          varchar uses the database character set to store the data, where as nvarchar uses the national character set to store the data.

          varchar can have max 2000 length same as nvarchar. but difference is varchar is single byte representation. so it will have 2000 bytes. where as we can give 2000 as length for nvarchar. but it will have 4000 bytes (because each character takes two bytes).

          I hope you understand the difference.

          Thanks,
          -S
          • 2. Re: Difference between VARCHAR & NVARCHAR using data type code
            Vincent Rogier
            To find out if a column is varchar or nvarchar, you need to call OCIAttrGet() with the attribute OCI_ATTR_CHARSET_FORM
            • 3. Re: Difference between VARCHAR & NVARCHAR using data type code
              ddevienne
              varchar uses the database character set to store the
              data, where as nvarchar uses the national character
              set to store the data.
              Yep.
              varchar can have max 2000 length same as nvarchar.
              but difference is varchar is single byte representation.
              That's contradicting your first sentence above. varchar will use whatever encoding the DB charset specifies, which can be multi-bytes.
              so it will have 2000 bytes. where as
              we can give 2000 as length for nvarchar. but it will
              have 4000 bytes (because each character takes two bytes).
              Again this is incorrect IMHO. Sure, nchar or nvarchar will take 2 bytes per character if you use UTF-16 as the DB national charset (ignoring surrogate pairs for chars beyond the BMP...), but you can use any encoding for nchar and co, you choose, and it's not necessarily UTF-16.

              Finally, let me add that you shouldn't care that much about which charsets is used, or whether the column is char/nchar or varchar/nvarchar, just tell the OCI client which charset you want for strings, and it will convert them for you. Just pick a good encoding to always use client side (typically one of the UTF one, 8, 16, or 32) that can represent any strings, initialize the OCI env accordingly, and OCI will do the rest. --DD