ORACLE Char and VARCHAR2 types

Source: Internet
Author: User

Today comes into contact with a more interesting question, common to extremely easy to ignore, but inadvertently fell out of the pit and not easy to come out.

To create a table:

CREATE TABLE Temp_decode (Borrow_type char (1), Borrow_type1 char (2), Borrow_type2 VARCHAR2 (Ten), Borrow_type3 INT)

  

Execute SQL as follows:

SELECT DECODE (Borrow_type, ' 7 ', ' abc ', ' Hello '), DECODE (Borrow_type1, ' 7 ', ' abc ', ' Hello '), DECODE (borrow_type1,7, ' abc ') , ' Hello '), DECODE (Borrow_type2, ' 7 ', ' abc ', ' Hello '), DECODE (Borrow_type3, ' 7 ', ' abc ', ' Hello ') from Temp_decode

  

What do you think the results will look like?

Actually the result is this guy, is it the same as you expected?

When we think about it, we all know that the char type is fixed (in the pit), so the ' 7 ' in char (2) type is not ' 7 ', but the database fills a character, so what is the problem?

is actually a space (""), as in this SQL

SELECT  DECODE (Borrow_type, ' 7 ', ' abc ', ' Hello '), DECODE (Borrow_type1, ' 7 ', ' abc ', ' Hello '),--note the space of this place DECODE ( borrow_type1,7, ' abc ', ' Hello '), DECODE (Borrow_type2, ' 7 ', ' abc ', ' Hello '), DECODE (Borrow_type3, ' 7 ', ' abc ', ' Hello ') From Temp_decode  

The result is:

Although this problem is not common, it is very easy to drop the pit if it is an enumeration type such as (1,2,3,4.). such as in the DB suggest or use smallint bar, if the non-character of the VARCHAR2 type bar, char type or as little as possible. Imagine that you originally char (1) with a good,

As a result, the length of the other people changed to char (2), your previous SQL is not directly duang (if the stored values you directly written as a numeric type can also like the first SQL, just this scenario I do not know why you do not smallint it)?

ORACLE Char and VARCHAR2 types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.