Oracle PL/SQL/Data Type/CHAR

Материал из SQL эксперт
Перейти к: навигация, поиск

CHAR() and VARCHAR2()

   
SQL> CREATE TABLE test_2 (
  2       name   VARCHAR2(20),
  3       gender CHAR(1)
  4       );

Table created.
SQL>
SQL> INSERT INTO test_2 VALUES ("George", "M");
1 row created.
SQL> INSERT INTO test_2 VALUES ("Jane", "F");
1 row created.
SQL>
SQL> SELECT * FROM test_2;
NAME                 G
-------------------- -
George               M
Jane                 F
SQL>
SQL> DROP TABLE test_2;
Table dropped.
SQL>
SQL>



char type and varchar2 type

    

SQL> create table myTable(
  2      codeValue   char(2) not null,
  3      address     varchar2(30)
  4  );
Table created.
SQL>
SQL> desc myTable;
 Name                      Null?    Type
 ------------------------- -------- ------------------
 CODEVALUE                 NOT NULL CHAR(2)
 ADDRESS                            VARCHAR2(30)
SQL>
SQL> insert into myTable values ("AZ","Arizona");
1 row created.
SQL> insert into myTable (codeValue, address) values ("NJ","New Jersey");
1 row created.
SQL> insert into myTable (codeValue, address) values ("CA","California");
1 row created.
SQL> insert into myTable (codeValue, address) values ("TX","Texas");
1 row created.
SQL> insert into myTable (codeValue, address) values ("FL","Florida");
1 row created.
SQL> insert into myTable (codeValue, address) values ("MN","Maine");
1 row created.
SQL>
SQL> select * from myTable;

CO  ADDRESS
--  ------------------------------
AZ  Arizona
NJ  New Jersey
CA  California
TX  Texas
FL  Florida
MN  Maine
6 rows selected.
SQL>
SQL> select address from myTable;

ADDRESS
------------------------------
Arizona
New Jersey
California
Texas
Florida
Maine
6 rows selected.
SQL>
SQL>
SQL> drop table myTable;
Table dropped.
SQL>



char type with 50 characters

   
SQL>
SQL> create table MyTable(
  2        bean_name char(50 char)
  3  );
Table created.
SQL>
SQL> desc MyTable;
 Name                                                                                                      Null?    Type
 ----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
 BEAN_NAME                                                                                                          CHAR(50 CHAR)
SQL>
SQL> drop table MyTable;
Table dropped.
SQL>



Compare char value without knowing the case

  
SQL> create table emp
  2  ( empno      NUMBER(4)    constraint E_PK primary key
  3  , ename      VARCHAR2(8)
  4  , init       VARCHAR2(5)
  5  , job        VARCHAR2(8)
  6  , mgr        NUMBER(4)
  7  , bdate      DATE
  8  , sal       NUMBER(6,2)
  9  , comm       NUMBER(6,2)
 10  , deptno     NUMBER(2)    default 10
 11  ) ;
Table created.
SQL> insert into emp values(1,"Tom","N",   "Coder", 13,date "1965-12-17",  800 , NULL,  20);
1 row created.
SQL> insert into emp values(2,"Jack","JAM", "Tester",6,date "1961-02-20",  1600, 300,   30);
1 row created.
SQL> insert into emp values(3,"Wil","TF" ,  "Tester",6,date "1962-02-22",  1250, 500,   30);
1 row created.
SQL> insert into emp values(4,"Jane","JM",  "Designer", 9,date "1967-04-02",  2975, NULL,  20);
1 row created.
SQL> insert into emp values(5,"Mary","P",  "Tester",6,date "1956-09-28",  1250, 1400,  30);
1 row created.
SQL> insert into emp values(6,"Black","R",   "Designer", 9,date "1963-11-01",  2850, NULL,  30);
1 row created.
SQL> insert into emp values(7,"Chris","AB",  "Designer", 9,date "1965-06-09",  2450, NULL,  10);
1 row created.
SQL> insert into emp values(8,"Smart","SCJ", "Coder", 4,date "1959-11-26",  3000, NULL,  20);
1 row created.
SQL> insert into emp values(9,"Peter","CC",   "Designer",NULL,date "1952-11-17",  5000, NULL,  10);
1 row created.
SQL> insert into emp values(10,"Take","JJ", "Tester",6,date "1968-09-28",  1500, 0,     30);
1 row created.
SQL> insert into emp values(11,"Ana","AA",  "Coder", 8,date "1966-12-30",  1100, NULL,  20);
1 row created.
SQL> insert into emp values(12,"Jane","R",   "Manager",   6,date "1969-12-03",  800 , NULL,  30);
1 row created.
SQL> insert into emp values(13,"Fake","MG",   "Coder", 4,date "1959-02-13",  3000, NULL,  20);
1 row created.
SQL> insert into emp values(14,"Mike","TJA","Manager",   7,date "1962-01-23",  1300, NULL,  10);
1 row created.
SQL>
SQL> select lower(job), initcap(ename)
  2  from   emp
  3  where  upper(job) = "Tester"
  4  order  by length(ename);
no rows selected
SQL>
SQL> drop table emp;
Table dropped.



Comparison of CHAR with VARCHAR2

  
SQL> -- Comparison of CHAR with VARCHAR2.
SQL>
SQL> SET SERVEROUTPUT ON
SQL> SET ECHO ON
SQL>
SQL> DECLARE
  2    employee_name_c CHAR(32);
  3    employee_name_v VARCHAR2(32);
  4  BEGIN
  5    -- Assign the same value to each string.
  6    employee_name_c := "String";
  7    employee_name_v := "String";
  8
  9    -- Test the strings for equality.
 10    IF employee_name_c = employee_name_v THEN
 11       DBMS_OUTPUT.PUT_LINE("The names are the same");
 12    ELSE
 13       DBMS_OUTPUT.PUT_LINE("The names are NOT the same");
 14    END IF;
 15  END;
 16  /
The names are NOT the same
PL/SQL procedure successfully completed.
SQL>



Convert string to clob

  
SQL>
SQL> declare
  2
  3    c1 clob := to_clob("abc");
  4    c2 clob;
  5
  6  begin
  7      case c1
  8           when to_clob("abc") then dbms_output.put_line("abc");
  9           when to_clob("def") then dbms_output.put_line("def");
 10      end case;
 11
 12      c2 := case c1
 13         when to_clob("abc") then "abc"
 14         when to_clob("def") then "def"
 15      end;
 16
 17      dbms_output.put_line(c2);
 18
 19  end;
 20  /
abc
abc
PL/SQL procedure successfully completed.
SQL>



Define and use char value

  
SQL>
SQL> -- Using IF...ELSIF to determine a grade.
SQL> DECLARE
  2     variable_Score Number := 85;
  3     variable_LetterGrade Char(1);
  4  BEGIN
  5     IF variable_Score >= 90 THEN
  6          variable_LetterGrade := "A";
  7     ELSIF variable_Score >= 80 THEN
  8          variable_LetterGrade := "B";
  9     ELSIF variable_Score >= 70 THEN
 10          variable_LetterGrade := "C";
 11     ELSIF variable_Score >= 60 THEN
 12          variable_LetterGrade := "D";
 13     ELSE
 14          variable_LetterGrade := "E";
 15     END IF;
 16          DBMS_OUTPUT.PUT_LINE("Your Letter Grade is: " || variable_LetterGrade);
 17  END;
 18  /
Your Letter Grade is: B
PL/SQL procedure successfully completed.
SQL>
SQL>



Define varchar2 type variable

  
SQL>
SQL> set serverout on
SQL>
SQL> declare
  2      l_text varchar2(100);
  3    begin
  4      l_text := "Hello, World!";
  5      dbms_output.put_line(l_text);
  6    exception
  7      when others then
  8        dbms_output.put_line("We encountered an exception!");
  9        raise;
 10    end;
 11    /
Hello, World!
PL/SQL procedure successfully completed.
SQL>



Memory allocation differences between the CHAR and VARCHAR2 datatypes

    
SQL>
SQL> DECLARE
  2    c CHAR(32767)     := " ";
  3    v VARCHAR2(32767) := " ";
  4  BEGIN
  5    dbms_output.put_line("c is ["||LENGTH(c)||"]");
  6    dbms_output.put_line("v is ["||LENGTH(v)||"]");
  7    v := v || " ";
  8    dbms_output.put_line("v is ["||LENGTH(v)||"]");  END;
  9  /
c is [32767]
v is [1]
v is [2]
PL/SQL procedure successfully completed.
SQL>



No equality comparison between two "" strings

   
SQL> select 12 from dual where "" = "";
no rows selected
SQL>
SQL>



"" represents a zero length character string BUT NOT a null value

   
SQL> select 12 from dual where null = "";
no rows selected
SQL>
SQL>



set column width by setting char number

   
SQL>
SQL> CREATE TABLE test_1 (
  2       first_name CHAR(15),
  3       last_name  CHAR(20)
  4       );

Table created.
SQL>
SQL> INSERT INTO test_1 VALUES ("Jane", "Smith");
1 row created.
SQL> INSERT INTO test_1 VALUES ("Christopher", "Allen");
1 row created.
SQL>
SQL> SELECT * FROM test_1;
FIRST_NAME      LAST_NAME
--------------- --------------------
Jane            Smith
Christopher     Allen
SQL>
SQL> DROP TABLE test_1;
Table dropped.
SQL>
SQL>



The CHAR data type is used for storing fixed-length character strings.

   
SQL> --
SQL>
SQL> create table MyTable(
  2        bean_name char(50)
  3  );
Table created.
SQL>
SQL> insert into MyTable values( "A " );
1 row created.
SQL> insert into MyTable values( " A" );
1 row created.
SQL> insert into MyTable values( "A" );
1 row created.
SQL>
SQL> select bean_name, length( bean_name )from MyTable;
BEAN_NAME                                          LENGTH(BEAN_NAME)
-------------------------------------------------- -----------------
A                                                                 50
 A                                                                50
A                                                                 50
SQL>
SQL> drop table MyTable;
Table dropped.
SQL>