Home > Database > Oracle > body text

How to convert Oracle data types

青灯夜游
Release: 2022-01-13 11:55:38
Original
20444 people have browsed it

Oracle data type conversion method: 1. Use the cast() function, the syntax "cast(x as type)"; 2. Use the to_char() function, the syntax "to_char(x[,f])" ;3. Use to_date() function, syntax "to_date(x[,f])", etc.

How to convert Oracle data types

The operating environment of this tutorial: Windows 7 system, Oracle 11g version, Dell G3 computer.

oracle’s data type conversion method

1. Use cast()

cast( ) is a data type conversion function. This function can convert x to the corresponding data type. It is basically used for numeric, character, and time types to install database rules for mutual conversion.

Syntax:

cast(x as type)
Copy after login

Example:

select 
 cast('123' as number) num,cast(123 as varchar2(3)) as ch,cast(to_date('20211112','yyyymmdd') as varchar2(12)) as time
from dual;
Copy after login

Result:

123
'123'
12-11月-21
Copy after login

2. Use to_char()

The to_char() function can format a string or time type x into a string according to format f.

Syntax:

to_char(x[,f])
Copy after login

Example:

select to_char(123.46,'999.9') from dual;
select to_char(sysdate,'yyyy-mm-dd') from dual;
Copy after login

Result:

 123.5
2021-11-13
Copy after login

3. Use to_date()

The to_date() function can format the string x according to format f and convert it into a time type result.

Syntax:

to_date(x[,f])
Copy after login

Example:

select to_date('2021-11-13','yyyy-mm-dd') from dual;
Copy after login

Result:

2021/11/13
Copy after login

4. Use to_number()

The to_number() function can format the string x according to format f and convert it into a numerical type result.

Grammar:

to_number(x[,f])
Copy after login

Example:

select to_number('123.74','999.99') from dual
Copy after login

Result:

123.74
Copy after login

Description: For the format f of the value, please refer to the following table

##99,9##$##Recommended tutorial: "Oracle Tutorial

Parameters

##Example

illustrate

9

##999


Return the number at the specified position


.


99.9


Specify the position of the decimal point


,



##Returns a comma at the specified position


$99.9

## Returns a dollar sign at the beginning

EEEE

##9.99EEEE


Specify scientific notation


The above is the detailed content of How to convert Oracle data types. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template