Cast number to a char : Cast « Conversion Functions « Oracle PL/SQL Tutorial






SQL>
SQL> CREATE TABLE book(
  2    title_id   CHAR(3)      NOT NULL,
  3    title_name VARCHAR(40)  NOT NULL,
  4    type       VARCHAR(10)  NULL    ,
  5    pub_id     CHAR(3)      NOT NULL,
  6    pages      INTEGER      NULL    ,
  7    price      DECIMAL(5,2) NULL    ,
  8    sales      INTEGER      NULL    ,
  9    pubdate    DATE         NULL    ,
 10    contract   SMALLINT     NOT NULL
 11  );

Table created.

SQL>
SQL>
SQL>
SQL>
SQL> INSERT INTO book VALUES('T01','Java','history','P01',111,21.99,566,DATE '2000-08-01',1);

1 row created.

SQL> INSERT INTO book VALUES('T02','Oracle','history','P03', 114,19.95,9566,DATE '1998-04-01',1);

1 row created.

SQL> INSERT INTO book VALUES('T03','SQL','computer','P02', 122,39.95,25667,DATE '2000-09-01',1);

1 row created.

SQL> INSERT INTO book VALUES('T04','C++','psychology','P04', 511,12.99,13001,DATE '1999-05-31',1);

1 row created.

SQL> INSERT INTO book VALUES('T05','Python','psychology','P04', 101,6.95,201440,DATE '2001-01-01',1);

1 row created.

SQL> INSERT INTO book VALUES('T06','JavaScript','biography','P01', 173,19.95,11320,DATE '2000-07-31',1);

1 row created.

SQL> INSERT INTO book VALUES('T07','LINQ','biography','P03', 331,23.95,1500200,DATE '1999-10-01',1);

1 row created.

SQL> INSERT INTO book VALUES('T08','C#','children','P04', 861,10.00,4095,DATE '2001-06-01',1);

1 row created.

SQL> INSERT INTO book VALUES('T09','SQL Server','children','P04', 212,13.95,5000,DATE '2002-05-31',1);

1 row created.

SQL> INSERT INTO book VALUES('T10','AJAX','biography','P01', NULL,NULL,NULL,NULL,0);

1 row created.

SQL> INSERT INTO book VALUES('T11','VB','psychology','P04', 821,7.99,94123,DATE '2000-11-30',1);

1 row created.

SQL> INSERT INTO book VALUES('T12','Office','biography','P01', 507,12.99,100001,DATE '2000-08-31',1);

1 row created.

SQL> INSERT INTO book VALUES('T13','VBA','history','P03', 812,29.99,10467,DATE '1999-05-31',1);

1 row created.

SQL>
SQL>
SQL>
SQL>
SQL> SELECT CAST(sales AS CHAR(7))
  2         || ' copies sold of title '
  3         || title_id
  4           AS "Biography sales"
  5    FROM book
  6    WHERE type = 'biography'
  7      AND sales IS NOT NULL
  8    ORDER BY sales DESC;

Biography sales
--------------------------------
1500200 copies sold of title T07
100001  copies sold of title T12
11320   copies sold of title T06

SQL>
SQL> drop table book;

Table dropped.

SQL>








15.10.Cast
15.10.1.cast(12.98 as number(2)) example1
15.10.2.select cast('oak' as char(10) ) example2
15.10.3.select cast(null as date ) example3
15.10.4.Cast string to date type before comparison
15.10.5.Cast null as timestamp
15.10.6.Cast table of numbers
15.10.7.Cast number to a char
15.10.8.Cast date to char
15.10.9.Cast decimal to integer
15.10.10.Cast varchar to char
15.10.11.CAST examples
15.10.12.Use Date to cast string to date type