33.Oracle数据库SQL开发之 使用简单函数——使用单行函数字符函数

来源:互联网 发布:网络四大神书下载 编辑:程序博客网 时间:2024/06/05 15:45

33.Oracle数据库SQL开发之 使用简单函数——使用单行函数-字符函数

欢迎转载,转载请标明出处:    http://blog.csdn.net/notbaron/article/details/49722969     

这边开始使用数据库内置函数。函数可以接受零个或多个输入参数,并返回一个输出参数。ORACLE数据库中可以使用两种主要类型的函数:单行函数和聚合函数。

         单行函数(singlerow function)同时只能对一行进行操作,且对输入的每一行返回一个行输出结果。

         聚合函数(aggregatefunction),同时可以对多行操作,并返回一行输出结果。

单行函数主要有以下5种。

1.  字符函数

字符函数接受字符参数。例如 UPPER()将字符串中的所有字母全部转换成大写,并返回新的字符串。NVL()将一个空值转换成传递给该函数的值。

1.1             ASCII()和CHR()

ASCII函数用于获得字符的ASCII码。CHR函数用于获得ASCII码为X的字符。

例如:

store@PDB1> select ASCII('a'),ASCII('a'),ASCII(0)from dual;

 

ASCII('A') ASCII('A')  ASCII(0)

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

         97        97            48

store@PDB1> select chr(97),chr(96),chr(48) fromdual;

C C C

- - -

a ` 0

1.2             CONCAT()

CONCAT(X,Y)函数用于将y添加到x 之后,该函数会返回得到的字符串。

例如:

store@PDB1> select concat(first_name,last_name)from customers;

 

CONCAT(FIRST_NAME,LA

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

JohnBrown

CynthiaGreen

SteveWhite

GailBlack

DoreenBlue

CONCAT函数和||操作符的功能完成相同。

1.3             INITCAP()

INITCAP(X)函数用于将X中每个单词的首字母转换成大写。

例如:

store@PDB1> select product_id,initcap(description)from products where product_id<4;

 

PRODUCT_ID INITCAP(DESCRIPTION)

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

          1 A Description Of Modern Science

          2 Introduction To Chemistry

          3 A Star Explodes

store@PDB1> select product_id,description from products where product_id<4;

 

PRODUCT_ID DESCRIPTION

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

          1 A description of modern science

          2 Introduction to Chemistry

          3 A star explodes

1.4             INSTR()

INSTR(X,find_string[,start] [,occurrence])函数用于在x中查询find_string.返回find_string所在的位置。Start说明从哪个位置开始查找,occurrence说明返回find_string第几次出现的位置。

例如:

store@PDB1> select name,instr(name,'Science') fromproducts where product_id=1;

 

NAME                                INSTR(NAME,'SCIENCE')

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

Modern Science                                                8

以下查询使用INSTR函数显示从产品名的开头开始第二次出现字母e的位置

store@PDB1> select name,instr(name,'e',1,2) fromproducts where product_id=1;

 

NAME                                INSTR(NAME,'E',1,2)

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

Modern Science                                             11

下面查询,使用INSTR函数显示第一个顾客的dob 中字符串JAN的位置

store@PDB1> select customer_id,dob,instr(dob,'JAN')from customers where customer_id = 1;

CUSTOMER_ID DOB       INSTR(DOB,'JAN')

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

           1 01-JAN-65               4

1.5             LENGTH()

Length(x)函数用于获得x字符中的个数。

例如:

store@PDB1> select name,length(name) fromproducts;

 

NAME                                 LENGTH(NAME)

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

Modern Science                                   14

Chemistry                                       9

Supernova                                      9

Tank War                               8

Z Files                                    7

2412: The Return                       16

Space Force 9                              13

From Another Planet                          19

Classical Music                           15

Pop 3                                              5

Creative Yell                                 13

My Front Line                              13

 

12 rows selected.

小数点也是在总数之内的如下:

store@PDB1> select price,length(price) fromproducts where product_id < 3;

    PRICE LENGTH(PRICE)

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

    19.95             5

         30           2

1.6             LOWER()和UPPER()

Lower(x)将x 中的字母转换成小写。UPPER(x)函数将x 中的字母转换成大写。

1.7             LPAD()和RPAD()

LPAD(x,width,[,pad_string])函数用于在x的左边补齐空格,使x的总长度达到width个字符。RPAD同理,在X的右边补齐字符串。

例如:

store@PDB1> selectRPAD(name,30,'.'),LPAD(price,8,'*+') from products where product_id < 4;

 

RPAD(NAME,30,'.')         LPAD(PRI

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

Modern Science................ *+*19.95

Chemistry..................... *+*+*+30

Supernova..................... *+*25.99

1.8             LTRIM(),RTRIM()和TRIM()

LTRIM(x,[trim_string])函数用于从x的左边截去一些字符。如果没有指定trim_string参数,默认会截去空格。同理RTRIM函数从右边截去一些字符。

store@PDB1> select ltrim(' Hello GailSeymour!'),RTRIM('Hi Dreen Oakley!abcabc', 'abc'),trim('0' from '000Hey SteveButton!0000')

  2  from dual;

 

LTRIM('HELLOGAILSEY RTRIM('HIDREENOATRIM('0'FROM'000H

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

Hello Gail Seymour! Hi Dreen Oakley! HeySteve Button!

1.9             NVL()

NVL函数将空值转换成一个已知的值。NVL(x,value)的结果如下:如果x为空,则返回value,否则返回x.

例如:

store@PDB1> select customer_id,NVL(phone,'UnknownPhone Number') from customers;

 

CUSTOMER_ID NVL(PHONE,'UNKNOWNPH

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

           1 800-555-1211

           2 800-555-1212

           3 800-555-1213

           4 800-555-1214

           5 Unknown Phone Number

1.10       NVL2()

NVL2(x,value1,value2)的返回结果如下:如果X非空,返回VALUE1,否则返回VALUE2。

执行如下:

store@PDB1> select customer_id,nvl2( phone,'known','Unkown')from customers;

CUSTOMER_ID NVL2(P

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

           1 known

           2 known

           3 known

           4 known

           5 Unkown

1.11       REPLACE()

REPLACE(x,search_string,replace_string)用于在x中查询search_string,并将其替换为replace_string.

执行如下:

store@PDB1> select replace(name,'Science','Physics') from products where product_id=1;

 

REPLACE(NAME,'SCIENCE','PHYSICS')

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

Modern Physics

该函数并不会真正对数据库中的数据进行修改,而是只对结果集中返回的行进行修改。

1.12       SOUNDEX()

SOUNDEX(x)用于获得包含x发音的一个字符串。该函数用于对英文拼写不同但发音相似的单词进行比较。

例如从customers表中选择last_name发音类似于whyte的last_name列:

store@PDB1> select last_name from customers wheresoundex(last_name)=soundex('whyte');

 

LAST_NAME

----------

White

1.13       SUBSTR()

SUBSTR(X,START[,length])用于从x中取得从start位置开始的一个子字符串,可以使用length指定字符串长度。

例如:

store@PDB1> select substr(name,2,7) from productswhere product_id < 4;

 

SUBSTR(

-------

odern S

hemistr

upernov

1.14       函数中使用表达式

函数中并非只能使用表中的列,也可以提供任意有效的表达式,条件是这个表达式的结果是一个字符串。

1.15       函数组合

在一个SQL语句中个,可以使用函数的任意有效组合。

例如如下:

store@PDB1> select name,upper(substr(name,2,8))from products where product_id < 4;

 

NAME                                UPPER(SU

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

Modern Science                        ODERNSC

Chemistry                         HEMISTRY

Supernova                         UPERNOVA

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0 0
原创粉丝点击