What are the oracle conversion functions?
oracle的转换函数有:to_char()、to_date()、to_number()、CAST()、TO_MULTI_BYTE()、to_single_byte()、CONVERT()、UNISTR()、COMPOSE()等等。
本教程操作环境:Windows7系统、Oracle 11g版、Dell G3电脑。
转换函数可用于将一种数据类型转换为另一种数据类型。
oracle转换函数有哪些
1、to_char()函数:将DATE或者NUMBER转换为字符串
2、 to_date()函数:将number、char转换为date
3、 to_number()函数:将char转换为number
4、CAST(expr AS type_name)函数:用于将一个内置数据类型或集合类型转变为另一个内置数据类型或集合类型。
expr为列名或值,type_name数据类型。
SELECT CAST('123.4' AS int) from dual;
结果:123
可进行四舍五入操作:
SELECT CAST('123.447654' AS decimal(5,2)) as result from dual;
decimal(5,2)表示值总位数为5,精确到小数点后2位。
结果:123.45
5、TO_MULTI_BYTE(c1)函数:将字符串c1中的半角转化为全角。TO_MULTI_BYTE和TO_SINGLE_BYTE是相反的两个函数.
select to_multi_byte('高A') text from dual;
结果:
6、to_single_byte(c1)函数:将字符串c1中的全角转化为半角。
select to_single_byte('高A') text from dual;
结果:
7、TIMESTAMP_TO_SCN(timestamp)函数:用于根据输入的timestamp返回所对应的scn值,其中timestamp用于指定日期时间。
作为对于闪回操作(flashback)的一个增强,Oracle10g提供了函数对于SCN和时间戳进行相互转换。
select timestamp_to_scn(sysdate) scn from dual;
结果:9709105
8、SCN_TO_TIMESTAMP(number)函数:根据输入的scn值返回对应的大概日期时间,其中number用于指定scn值。
select to_char(scn_to_timestamp(9709105), 'yyyy-mm-dd hh24:mi:ss')from dual;
结果:2018-05-21 18:23:35
9、CONVERT(string,dest_set[,source_set])函数:将字符串string从source_set所表示的字符集转换为由dest_set所表示的字符集.如果source_set没有被指定,它缺省的被设置为数据库的字符集。
ZHS16GBK表示采用GBK编码格式、16位(两个字节)简体中文字符集
WE8ISO8859P1(西欧、8位、ISO标准8859P1编码)
AL32UTF8(其中AL代表ALL,指适用于所有语言)、zhs16cgb231280
select convert('中国','US7ASCII','WE8ISO8859P1') "conversion" from dual;
结果:O??u
10、TRANSLATE(str1 USING zfj)函数:将字符串转变为数据库字符集(char_cs)或民族字符集(nchar_cs)
Select TRANSLATE(‘中国’ using nchar_cs) from dual;
结果:中国
11、ASCIISTR(s)函数:将任意字符集的字符串转变为数据库字符集的ASCII字符串。
Select ASCIISTR ('1A_中文') from dual;
结果:1A_\4E2D\6587
12、UNISTR(str1)函数:输入字符串返回相应的UNICODE字符
Select UNISTR ('\4E2D'),UNISTR ('\6587'),UNISTR ('\0300'),UNISTR ('\00E0') from dual;
结果:中 文 ` à
13、COMPOSE(string)函数:这个函数以UNICODE字符串为参数,返回一个规范化的字符串。
比如,它可以接受一个字母和一个组合标记,比如说‘a'(Unicode 字符0097)和沉音符(Unicode 字符0300),然后创建一个单独的由两个标记组合而成的字符(à)。
Select COMPOSE('a'||unistr('\0300')) from dual;
结果:à
14、DECOMPOSE(string)函数:返回一个Unicode字符串。它是string的规范分解。
SELECT DECOMPOSE ('Châteaux') FROM DUAL;
结果:Cha^teaux
15、CHARTOROWID(c1)函数:将字符数据类型CHAR或VARCHAR2转换为ROWID值.参数c1是长度为18的字符串,必须符合rowid格式.CHARTOROWID是ROWIDTOCHAR的反函数。
在Oracle中,每一条记录都有一个rowid,rowid在整个数据库中是唯一的,rowid确定了每条记录是在Oracle中的哪一个数据文件、块、行上。在重复的记录中,可能所有列的内容都相同,但rowid不会相同。
SELECT chartorowid('AAAADeAABAAAAZSAAA') FROM DUAL;
结果:AAAADeAABAAAAZSAAA
16、ROWIDTOCHAR(rowid)函数:转换rowid值为varchar2类型,rowid固定参数,返回长度为18的字符串。
SELECT ROWIDTOCHAR(rowid) FROM DUAL;
结果:AAAAB0AABAAAAOhAAA
17、INTERVAL 'integer [- integer]' {YEAR | MONTH} [(precision)][TO {YEAR | MONTH}]函数:该数据类型常用来表示一段时间差,注意时间差只精确到年和月。
precision为年或月的精确域, 有效范围是0到9, 默认值为2.
select INTERVAL '123-2' YEAR(3) TO MONTH from dual;
表示: 123年2个月, "YEAR(3)" 表示年的精度为3, 可见"123"刚好为3为有效数值, 如果该处YEAR(n), n<3就会出错, 注意默认是2
结果:+123-02
18、HEXTORAW(string)函数:将string一个十六进制构成的字符串转换为二进制RAW数值.String中的每两个字符表示了结果RAW中的一个字节.HEXTORAW和RAWTOHEX为相反的两个函数.当出现比f大的字母时(以a最小z最大)就会报错
select hextoraw('abcdef') from dual;
结果:ABCDEF
19、RAWTOHEX(rawvalue)函数:将raw串转换为十六进制. rawvalue中的每个字节都被转换为一个双字节的字符串.
select rawtohex('AA') from dual;
结果:4141 结果之所以是4141是因为A的ASCII为65,65转换为十六进制就是41。
20、TO_LOB (long_column)函数:将LONG或LONG ROW列的数据转变为相应的LOB类型。
但需要注意的是,TO_LOB一般只用在CREATE TABLE或INSERT TABLE语句后面的子查询中。在其他地方使用会报错,比如UPDATE语句。
推荐教程:《Oracle教程》
The above is the detailed content of What are the oracle conversion functions?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To query the Oracle tablespace size, follow the following steps: Determine the tablespace name by running the query: SELECT tablespace_name FROM dba_tablespaces; Query the tablespace size by running the query: SELECT sum(bytes) AS total_size, sum(bytes_free) AS available_space, sum(bytes) - sum(bytes_free) AS used_space FROM dba_data_files WHERE tablespace_

Data import method: 1. Use the SQLLoader utility: prepare data files, create control files, and run SQLLoader; 2. Use the IMP/EXP tool: export data, import data. Tip: 1. Recommended SQL*Loader for big data sets; 2. The target table should exist and the column definition matches; 3. After importing, data integrity needs to be verified.

Creating an Oracle table involves the following steps: Use the CREATE TABLE syntax to specify table names, column names, data types, constraints, and default values. The table name should be concise and descriptive, and should not exceed 30 characters. The column name should be descriptive, and the data type specifies the data type stored in the column. The NOT NULL constraint ensures that null values are not allowed in the column, and the DEFAULT clause specifies the default values for the column. PRIMARY KEY Constraints to identify the unique record of the table. FOREIGN KEY constraint specifies that the column in the table refers to the primary key in another table. See the creation of the sample table students, which contains primary keys, unique constraints, and default values.

Uninstall method for Oracle installation failure: Close Oracle service, delete Oracle program files and registry keys, uninstall Oracle environment variables, and restart the computer. If the uninstall fails, you can uninstall manually using the Oracle Universal Uninstall Tool.

There are three ways to view instance names in Oracle: use the "sqlplus" and "select instance_name from v$instance;" commands on the command line. Use the "show instance_name;" command in SQL*Plus. Check environment variables (ORACLE_SID on Linux) through the operating system's Task Manager, Oracle Enterprise Manager, or through the operating system.

Oracle View Encryption allows you to encrypt data in the view, thereby enhancing the security of sensitive information. The steps include: 1) creating the master encryption key (MEk); 2) creating an encrypted view, specifying the view and MEk to be encrypted; 3) authorizing users to access the encrypted view. How encrypted views work: When a user querys for an encrypted view, Oracle uses MEk to decrypt data, ensuring that only authorized users can access readable data.

Oracle provides multiple deduplication query methods: The DISTINCT keyword returns a unique value for each column. The GROUP BY clause groups the results and returns a non-repetitive value for each group. The UNIQUE keyword is used to create an index containing only unique rows, and querying the index will automatically deduplicate. The ROW_NUMBER() function assigns unique numbers and filters out results that contain only line 1. The MIN() or MAX() function returns non-repetitive values of a numeric column. The INTERSECT operator returns the common values of the two result sets (no duplicates).

Use the ALTER TABLE statement, the specific syntax is as follows: ALTER TABLE table_name ADD column_name data_type [constraint-clause]. Where: table_name is the table name, column_name is the field name, data_type is the data type, and constraint-clause is an optional constraint. Example: ALTER TABLE employees ADD email VARCHAR2(100) Add an email field to the employees table.
