Oracle (CAST, TO_CHAR, to_date) usage

Source: Internet
Author: User

Cast:

Cast (the value to be converted as the type of the conversion)

from to Number
to Binary_float, binary_double to CHAR, VARCHAR2 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

Related Article

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.