SQL>
SQL>
SQL> -- create demo table
SQL> create table Employee(
2 ID VARCHAR2(4 BYTE) NOT NULL,
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /
Table created.
SQL>
SQL> -- prepare data
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values ('01','Jason', 'Martin', to_date('19960725','YYYYMMDD'), to_date('20060725','YYYYMMDD'), 1234.56, 'Toronto', 'Programmer')
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values('02','Alison', 'Mathews', to_date('19760321','YYYYMMDD'), to_date('19860221','YYYYMMDD'), 6661.78, 'Vancouver','Tester')
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values('03','James', 'Smith', to_date('19781212','YYYYMMDD'), to_date('19900315','YYYYMMDD'), 6544.78, 'Vancouver','Tester')
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values('04','Celia', 'Rice', to_date('19821024','YYYYMMDD'), to_date('19990421','YYYYMMDD'), 2344.78, 'Vancouver','Manager')
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values('05','Robert', 'Black', to_date('19840115','YYYYMMDD'), to_date('19980808','YYYYMMDD'), 2334.78, 'Vancouver','Tester')
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values('06','Linda', 'Green', to_date('19870730','YYYYMMDD'), to_date('19960104','YYYYMMDD'), 4322.78,'New York', 'Tester')
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values('07','David', 'Larry', to_date('19901231','YYYYMMDD'), to_date('19980212','YYYYMMDD'), 7897.78,'New York', 'Manager')
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values('08','James', 'Cat', to_date('19960917','YYYYMMDD'), to_date('20020415','YYYYMMDD'), 1232.78,'Vancouver', 'Tester')
3 /
1 row created.
SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
2 /
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester
03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester
04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester
06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester
07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager
08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester
8 rows selected.
SQL>
SQL>
SQL> SELECT id, 'The salary of this employee is' || TO_CHAR(salary, '$99999999.99')
2 FROM employee;
ID 'THESALARYOFTHISEMPLOYEEIS'||TO_CHAR(SALARY
---- -------------------------------------------
01 The salary of this employee is $1234.56
02 The salary of this employee is $6661.78
03 The salary of this employee is $6544.78
04 The salary of this employee is $2344.78
05 The salary of this employee is $2334.78
06 The salary of this employee is $4322.78
07 The salary of this employee is $7897.78
08 The salary of this employee is $1232.78
8 rows selected.
SQL>
SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
2 /
Table dropped.
SQL>
15.7.TO_CHAR |
| 15.7.1. | Format Parameters |
| 15.7.2. | TO_CHAR(x [, format]) converts x to a string. |
| 15.7.3. | TO_CHAR() will return a string of pound characters (#) if you try and format a number that contains too many digits for the format you have provided. |
| 15.7.4. | Use TO_CHAR() to convert columns containing numbers to strings. |
| 15.7.5. | TO_CHAR(12345.67, '99,999.99') (1) |
| 15.7.6. | TO_CHAR(12345.67, '99999.99') (2) |
| 15.7.7. | TO_CHAR(-12345.67, '99,999.99') (3) |
| 15.7.8. | TO_CHAR(12345.67, '099,999.99') (4) |
| 15.7.9. | TO_CHAR(12345.67, '99,999.9900') |
| 15.7.10. | TO_CHAR(12345.67, '$99,999.99') |
| 15.7.11. | TO_CHAR(0.67, 'B9.99') |
| 15.7.12. | TO_CHAR(12345.67, 'C99,999.99') |
| 15.7.13. | TO_CHAR(12345.67, '99999D99') |
| 15.7.14. | TO_CHAR(12345.67, '99999.99EEEE') |
| 15.7.15. | TO_CHAR(0012345.6700, 'FM99999.99') |
| 15.7.16. | TO_CHAR(12345.67, '99999G99') |
| 15.7.17. | TO_CHAR(12345.67, 'L99,999.99') |
| 15.7.18. | TO_CHAR(-12345.67, '99,999.99MI') |
| 15.7.19. | TO_CHAR(-12345.67, '99,999.99PR') |
| 15.7.20. | TO_CHAR(2007, 'RN') |
| 15.7.21. | TO_CHAR(12345.67, 'TM') |
| 15.7.22. | TO_CHAR(12345.67, 'U99,999.99') |
| 15.7.23. | TO_CHAR(12345.67, '99999V99') |
| 15.7.24. | TO_CHAR(last_ddl_time,'dd-mon-yyyy hh24:mi') |
| 15.7.25. | to_char(sysdate,'hh24:mi:ss') as time |
| 15.7.26. | to_char(to_date('01/01/2006','dd/mm/yyyy'),is on Day') as new_year_2006 |
| 15.7.27. | to_char(sysdate, 'Day', 'nls_date_language=Dutch') |
| 15.7.28. | to_char(sysdate,'DAY dy Dy') |
| 15.7.29. | to_char(sysdate,'MONTH mon') as month |
| 15.7.30. | to_char(bdate,'fmMonth ddth, yyyy') |
| 15.7.31. | use to_char more than once to create long date format |