Cast:
Cast (the value to be converted as the type of the conversion)
| from
to Binary_float, binary_double |
to CHAR, VARCHAR2 |
| to Number
to DATETIME, INTERVAL |
to NCHAR, NVARCHAR2 |
Binary_float, binary_double |
Yes |
Yes |
Yes |
No |
Yes |
CHAR, VARCHAR2 |
Yes |
Yes |
Yes |
Yes |
No |
Number |
Yes |
Yes |
Yes |
No |
Yes |
DATETIME, INTERVAL |
No |
Yes |
No |
Yes |
Yes |
NCHAR, NVARCHAR2 |
Yes |
No |
Yes |
No |
Yes |
Example
CAST(‘123.4567‘ AS NUMBER(10,2))
Returns the value 123.46
.
From the above description, we can know that cast can convert one type to another.
For example, converting a string type to a number (10,2) type, and not just to using To_number, To_char (), and to_date () types, table 3-1 above represents whether cast can be used for this type of mutual conversions.
Example calls:
Chenzw> SELECT CAST (' 123.4567 ' as Number (10,2)) as NUM from DUAL; Output 123.46
To_char:
Convert to String type
SELECT to_char (sysdate, ' Yyyy/mm/dd HH24:mi:ss ') from DUAL
Sysdate: The value to be converted, followed by the type of the spin (string type)
function |
return |
Description |
Example |
To_char (timestamp, text) |
Text |
Convert Timestamp to String |
To_char (timestamp ' Now ', ' HH12:MI:SS ') |
To_char (int, text) |
Text |
Convert Int4/int8 to String |
To_char (125, ' 999 ') |
To_char (Float, text) |
Text |
Convert Float4/float8 to String |
To_char (125.8, ' 999d9 ') |
To_char (Numeric, text) |
Text |
Convert numeric to String |
To_char (Numeric ' -125.8 ', ' 999d99s ') |
To_date (text, text) |
Date |
Convert String to date |
To_date (' Dec ', ' DD Mon YYYY ') |
To_timestamp (text, text) |
Date |
Convert String to Timestamp |
To_timestamp (' Dec ', ' DD Mon YYYY ') |
To_number (text, text) |
Numeric |
Convert string to Numeric |
To_number (' 12,454.8-', ' 99g999d9s ') |
To_date: Converts a value to a date type.
SELECT to_date (' 2007-06-12 10:00:00 ', ' yyyy/mm/dd HH24:mi:ss ') from DUAL to output 2007/6/12 Tuesday 10:00:00
Oracle (CAST, TO_CHAR, to_date) usage