自认为常用的Oracle函数(一)——字符函数

在用Oracle的时候经常会使用函数,上网查阅的时候很常查到的结果不是很让人满意,要么只有例子,要么只有解释,又或者太散,我在这里总结了一下这些函数:

 

一、字符函数

 

1、             LengthlengthB函数

Oracle中的字符函数中,有一类函数是求字符长度的函数,lengthlengthBlengthClength2length4几个函数中比较常用的是lengthlengthB

用法:Length(str)LengthB(str)

Length函数返回字符的个数,使用给定的字符集来计算字符的个数

LengthB给出该字符串的字节数

LengthC使用纯Unicode

Length2使用UCS2

Length4使用UCS4

例子:

SQL> Select length(’hello‘) from dual;

LENGTH(’hello‘)

————–—–—

5

一般不管你是什么字符集都将得到以上结果。

 

但是你的字符集却关系到如下结果。

SQL> Select lengthB(’你好‘) from dual;

LENGTHB(’你好‘)

————————

6

使用AL32UTF8字符集得到了以上结果。

SQL> Select lengthB(’你好‘) from dual;

LENGTHB(’你好‘)

————————

4

使用ZHS16GBK字符集得到了以上结果。

【相关知识点】

+查看数据库字符集
数据库服务器字符集select * from nls_database_parameters,其来源于props$,是表示数据库的字符集。

客户端字符集环境select * from nls_instance_parameters,其来源于v$parameter

表示客户端的字符集的设置,可能是参数文件,环境变量或者是注册表

会话字符集环境 select * from nls_session_parameters,其来源于v$nls_parameters,表示会话自己的设置,可能是会话的环境变量或者是alter session完成,如果会话没有特殊的设置,将与nls_instance_parameters一致。

客户端的字符集要求与服务器一致,才能正确显示数据库的非Ascii字符。如果多个设置存在的时候,alter session>环境变量>注册表>参数文件

字符集要求一致,但是语言设置却可以不同,语言设置建议用英文。如字符集是zhs16gbk,则nls_lang可以是American_America.zhs16gbk

+修改字符集
8i
以上版本可以通过alter database来修改字符集,但也只限于子集到超集,不建议修改props$表,将可能导致严重错误。
Startup nomount;
Alter database mount exclusive;
Alter system enable restricted session;
Alter system set job_queue_process=0;
Alter database open;
Alter database character set zhs16gbk;

 

2、             Trimltrimrtrim函数

1trim函数是用来删除给定字符串或者给定数字中的头部或者尾部的给定字符。

用法:trim([leading/trailing/both][匹配字符串或数值][from][需要被处理的字符串或数值])

这里如果指明了leading表示从删除头部匹配的字符串,如果指明了trailing表示从删除尾部匹配的字符串,如果指明了both,或者不指明任何位置,则两端都将被删除,如果不指明任何匹配字符串或数值则认为是空格,即删除前面或者后面的空格。

trim函数返回的类型是varchar2

例子:

截去了数字7500的后面的两个0

SQL> select trim(0 from 07500) from dual;

TRIM

—-

75

下面的例子截去了中秋八月中前后的两个

SQL> select trim(’‘ from ‘中秋八月中‘) from dual;

TRIM('

------

秋八月

2ltrimritrim函数就稍微麻烦一点点

用法ltrim(x,y)

函数是按照y中出现的字符一个一个截掉x中的字符,并且是从左边开始执行的,只要遇到y中有的字符, x中的字符都会被截掉, 直到在x的字符中遇到y中没有的字符为止函数命令才结束。【rtrim函数则和ltrim相反

 

例子:(看看下面的例子就会了解了)

SQL> select ltrim('109224323','109') from dual;

LTRIM('109224323','109')
------------------------
224323

 

这个的功能应该都知道的,再看一个:

 

SQL> select ltrim('10900094323','109') from dual;

LTRIM('10900094323','109')
---------------------------
4323

按道理说应该是00094323的结果,再来看两个对比的:

 

SQL> select ltrim('10900111000991110224323','109') from dual;

LTRIM('10900111000991110224323
------------------------------
224323

SQL> select ltrim('109200111000991110224323','109') from dual;

LTRIM('10920011100099111022432
------------------------------
200111000991110224323

为什么第二个查询语句多了一个2就没被截了呢?再来看一个:


SQL> select ltrim('902100111000991110224323','109') from dual;

LTRIM('90210011100099111022432
------------------------------
2100111000991110224323

按道理说是截109的值,为什么90也被截了呢?

 

以上问题的原因就是ltrim的用法问题,因为它截串是按字符的,不是按后面字符一整个串!

 

3、             Substr函数

用法:ubstr( string, start_position, [ length ] )
 取得字符串中指定起始位置和长度的字符串   
例子:
    select  substr('This is a test', 6, 2)    from dual
;    ----‘is’

    select  substr('This is a test', 6)   from dual;         ---- ‘is a test’
    select  substr('TechOnTheNet', -3, 3)  from dual;     ----  'Net'
    select  substr('TechOnTheNet', -6, 3)  from dual;    ----  'The'

select  substr('Thisisatest', -4, 2)  from dual;

 

4、             replacetranslate函数

这两个函数看官方的解释就很清楚用法了!

Replace

 
REPLACE(char, search_string
        [, replacement_string ]
       )

 

Purpose

REPLACE returns char with every occurrence of search_string replaced with replacement_string. If replacement_string is omitted or null, then all occurrences of search_string are removed. If search_string is null, then char is returned.

Both search_string and replacement_string, as well as char, can be any of the datatypes CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. The string returned is in the same character set as char. The function returns VARCHAR2 if the first argument is not a LOB and returns CLOB if the first argument is a LOB.

REPLACE provides functionality related to that provided by the TRANSLATE function. TRANSLATE provides single-character, one-to-one substitution. REPLACE lets you substitute one string for another as well as to remove character strings.

See Also:

TRANSLATE

 

Examples

The following example replaces occurrences of J with BL:

SELECT REPLACE('JACK and JUE','J','BL') "Changes FROM DUAL;

Changes

--------------

BLACK and BLUE

 

Translate

 

TRANSLATE(expr, from_string, to_string)

Purpose

TRANSLATE returns expr with all occurrences of each character in from_string replaced by its corresponding character in to_string. Characters in expr that are not in from_string are not replaced. If expr is a character string, then you must enclose it in single quotation marks. The argument from_string can contain more characters than to_string. In this case, the extra characters at the end of from_string have no corresponding characters in to_string. If these extra characters appear in char, then they are removed from the return value.

You cannot use an empty string for to_string to remove all characters in from_string from the return value. Oracle Database interprets the empty string as null, and if this function has a null argument, then it returns null.

TRANSLATE provides functionality related to that provided by the REPLACE function. REPLACE lets you substitute a single string for another single string, as well as remove character strings. TRANSLATE lets you make several single-character, one-to-one substitutions in one operation.

This function does not support CLOB data directly. However, CLOBs can be passed in as arguments through implicit data conversion.

See Also:

 

"Datatype Comparison Rules" for more information and REPLACE

Examples

The following statement translates a book title into a string that could be used (for example) as a filename. The from_string contains four characters: a space, asterisk, slash, and apostrophe (with an extra apostrophe as the escape character). The to_string contains only three underscores. This leaves the fourth character in the from_string without a corresponding replacement, so apostrophes are dropped from the returned value.

SELECT TRANSLATE('SQL*Plus User''s Guide', ' */''', '___') FROM DUAL;

TRANSLATE('SQL*PLUSU

--------------------

SQL_Plus_Users_Guide

select translate('liyan4h123ui','#liyanhui','#') from dual

结果:4123

select translate('asadad434323', '#0123456789','#') from dual

结果:asadad

select TRANSLATE('kkaxksx', 'kx', '12') from dual

结果:11a21s2

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值