Home > Database > Mysql Tutorial > Oracle+常用SQL+笔记

Oracle+常用SQL+笔记

WBOY
Release: 2016-06-07 17:07:34
Original
1110 people have browsed it

SQL中的单记录函数 1.ASCII返回与指定的字符对应的十进制数;SQLgt; Select Ascii(

SQL中的单记录函数

1.ASCII
返回与指定的字符对应的十进制数;
SQL> Select Ascii('A') A,Ascii('A') A,Ascii('0') Zero,Ascii(' ') Space From Dual;
A         A      ZERO     SPACE
--------- --------- --------- ---------
65        97        48        32

2.CHR
给出整数,返回对应的字符;
SQL> Select Chr(54740) Zhao,Chr(65) Chr65 From Dual;
ZH C
-- -
赵 A
3.CONCAT
连接两个字符串;
SQL> Select Concat('010-','88888888')||'转23'  高乾竞电话 From Dual;
高乾竞电话
----------------
010-88888888转23
4.INITCAP
返回字符串并将字符串的第一个字母变为大写;
SQL> Select Initcap('Smith') Upp From Dual;
UPP
-----
Smith

5.INSTR(C1,C2,I,J)
在一个字符串中搜索指定的字符,返回发现指定的字符的位置;
C1    被搜索的字符串
C2    希望搜索的字符串
I     搜索的开始位置,默认为1
J     出现的位置,默认为1
SQL> Select Instr('Oracle Traning','Ra',1,2) Instring From Dual;
INSTRING
---------
9

6.LENGTH
返回字符串的长度;
SQL> Select Name,Length(Name),Addr,Length(Addr),Sal,Length(To_char(Sal)) From .Nchar_tst;
NAME   LENGTH(NAME) ADDR             LENGTH(ADDR)       SAL LENGTH(TO_CHAR(SAL))
------ ------------ ---------------- ------------ --------- --------------------
高乾竞            3 北京市海锭区                6   9999.99                    7
 
7.LOWER
返回字符串,并将所有的字符小写
SQL> Select Lower('AaBbCcDd')AaBbCcDd From Dual;
AABBCCDD
--------
Aabbccdd

8.UPPER
返回字符串,并将所有的字符大写
SQL> Select Upper('AaBbCcDd') Upper From Dual;
UPPER
--------
AABBCCDD
 
9.RPAD和LPAD(粘贴字符)
RPAD  在列的右边粘贴字符
LPAD  在列的左边粘贴字符
SQL> Select Lpad(Rpad('Gao',10,'*'),17,'*')From Dual;
LPAD(RPAD('GAO',1
-----------------
*******Gao*******
不够字符则用*来填满

10.LTRIM和RTRIM
LTRIM  删除左边出现的字符串
RTRIM  删除右边出现的字符串
SQL> Select Ltrim(Rtrim('   Gao Qian Jing   ',' '),' ') From Dual;
LTRIM(RTRIM('
-------------
Gao Qian Jing

11.SUBSTR(String,Start,Count)
取子字符串,从Start开始,取Count个
SQL> Select Substr('13088888888',3,8) From Dual;
SUBSTR('
--------
08888888

12.REPLACE('String','S1','S2')
String   希望被替换的字符或变量
S1       被替换的字符串
S2       要替换的字符串
SQL> Select Replace('He Love You','He','I') From Dual;
REPLACE('HELOVEYOU','HE','I')
------------------------------
I Love You

13.SOUNDEX
返回一个与给定的字符串读音相同的字符串
SQL> Create Table Table1(Xm Varchar(8));
SQL> Insert Into Table1 Values('Weather');
SQL> Insert Into Table1 Values('Wether');
SQL> Insert Into Table1 Values('Gao');
SQL> Select Xm From Table1 Where Soundex(Xm)=Soundex('Weather');
XM
--------
Weather
Wether

14.TRIM('S' From 'String')
LEADING   剪掉前面的字符
TRAILING  剪掉后面的字符
如果不指定,默认为空格符
15.ABS
返回指定值的绝对值
SQL> Select Abs(100),Abs(-100) From Dual;
ABS(100) ABS(-100)
--------- ---------
100       100

16.ACOS
给出反余弦的值
SQL> Select Acos(-1) From Dual;
ACOS(-1)
---------
3.1415927

17.ASIN
给出反正弦的值
SQL> Select Asin(0.5) From Dual;
ASIN(0.5)
---------
.52359878

18.ATAN
返回一个数字的反正切值
SQL> Select Atan(1) From Dual;
ATAN(1)
---------
.78539816

19.CEIL
返回大于或等于给出数字的最小整数
SQL> Select Ceil(3.1415927) From Dual;
CEIL(3.1415927)
---------------
      4

20.COS
返回一个给定数字的余弦
SQL> Select Cos(-3.1415927) From Dual;
COS(-3.1415927)
---------------
     -1

21.COSH
返回一个数字反余弦值
SQL> Select Cosh(20) From Dual;
COSH(20)
---------
242582598

22.EXP
返回一个数字E的N次方根
SQL> Select Exp(2),Exp(1) From Dual;
EXP(2)    EXP(1)
--------- ---------
7.3890561 2.7182818

23.FLOOR
对给定的数字取整数
SQL> Select Floor(2345.67) From Dual;
FLOOR(2345.67)
--------------
  2345

24.LN
返回一个数字的对数值
SQL> Select Ln(1),Ln(2),Ln(2.7182818) From Dual;
LN(1)     LN(2) LN(2.7182818)
--------- --------- -------------
0 .69314718     .99999999

25.LOG(N1,N2)
返回一个以N1为底N2的对数
SQL> Select Log(2,1),Log(2,4) From Dual;
LOG(2,1)  LOG(2,4)
--------- ---------
0         2

26.MOD(N1,N2)
返回一个N1除以N2的余数
SQL> Select Mod(10,3),Mod(3,3),Mod(2,3) From Dual;
MOD(10,3)  MOD(3,3)  MOD(2,3)
--------- --------- ---------
1         0         2

27.POWER
返回N1的N2次方根
SQL> Select Power(2,10),Power(3,3) From Dual;
POWER(2,10) POWER(3,3)
----------- ----------
1024         27

28.ROUND和TRUNC
按照指定的精度进行舍入
SQL> Select Round(55.5),Round(-55.4),Trunc(55.5),Trunc(-55.5) From Dual;
ROUND(55.5) ROUND(-55.4) TRUNC(55.5) TRUNC(-55.5)
----------- ------------ ----------- ------------
 56          -55          55          -55

29.SIGN
取数字N的符号,大于0返回1,小于0返回-1,等于0返回0
SQL> Select Sign(123),Sign(-100),Sign(0) From Dual;
SIGN(123) SIGN(-100)   SIGN(0)
--------- ---------- ---------
1         -1         0

30.SIN
返回一个数字的正弦值
SQL> Select Sin(1.57079) From Dual;
SIN(1.57079)
------------
   1

31.SIGH
返回双曲正弦的值
SQL> Select Sin(20),Sinh(20) From Dual;
SIN(20)  SINH(20)
--------- ---------
.91294525 242582598

32.SQRT
返回数字N的根
SQL> Select Sqrt(64),Sqrt(10) From Dual;
SQRT(64)  SQRT(10)
--------- ---------
8 3.1622777

33.TAN
返回数字的正切值
SQL> Select Tan(20),Tan(10) From Dual;
TAN(20)   TAN(10)
--------- ---------
2.2371609 .64836083

34.TANH
返回数字N的双曲正切值
SQL> Select Tanh(20),Tan(20) From Dual;
TANH(20)   TAN(20)
--------- ---------
1 2.2371609
 
35.TRUNC
按照指定的精度截取一个数
SQL> Select Trunc(124.1666,-2) Trunc1,Trunc(124.16666,2) From Dual;
TRUNC1 TRUNC(124.16666,2)
--------- ------------------
100             124.16
 
36.ADD_MONTHS
增加或减去月份
SQL> Select To_char(Add_months(To_date('199912','Yyyymm'),2),'Yyyymm') From Dual;
TO_CHA
------
200002
SQL> Select To_char(Add_months(To_date('199912','Yyyymm'),-2),'Yyyymm') From Dual;
TO_CHA
------
199910

37.LAST_DAY
返回日期的最后一天
SQL> Select To_char(Sysdate,'Yyyy.Mm.Dd'),To_char((Sysdate)+1,'Yyyy.Mm.Dd') From Dual;
TO_CHAR(SY TO_CHAR((S
---------- ----------
2004.05.09 2004.05.10
SQL> Select Last_day(Sysdate) From Dual;
LAST_DAY(S
----------
31-5月 -04

38.MONTHS_BETWEEN(Date2,Date1)
给出Date2-Date1的月份
SQL> Select Months_between('19-12月-1999','19-3月-1999') Mon_between From Dual;
MON_BETWEEN
-----------
  9
SQL>Selectmonths_between(To_date('2000.05.20','Yyyy.Mm.Dd'),To_date('2005.05.20','Yyyy.Dd')) Mon_betw From Dual;
MON_BETW
---------
-60

39.NEW_TIME(Date,'This','That')
给出在This时区=Other时区的日期和时间
SQL> Select To_char(Sysdate,'Yyyy.Mm.Dd Hh24:Mi:ss') Bj_time,To_char(New_time
2  (Sysdate,'PDT','GMT'),'Yyyy.Mm.Dd Hh24:Mi:ss') Los_angles From Dual;
BJ_TIME             LOS_ANGLES
------------------- -------------------
2004.05.09 11:05:32 2004.05.09 18:05:32

40.NEXT_DAY(Date,'Day')
给出日期Date和星期X之后计算下一个星期的日期
SQL> Select Next_day('18-5月-2001','星期五') Next_day From Dual;
NEXT_DAY
----------
25-5月 -01
 
41.SYSDATE
用来得到系统的当前日期
SQL> Select To_char(Sysdate,'Dd-Mm-Yyyy Day') From Dual;
TO_CHAR(SYSDATE,'
-----------------
09-05-2004 星期日
Trunc(Date,Fmt)按照给出的要求将日期截断,如果Fmt='Mi'表示保留分,截断秒
SQL> Select To_char(Trunc(Sysdate,'Hh'),'Yyyy.Mm.Dd Hh24:Mi:ss') Hh,
  2  To_char(Trunc(Sysdate,'Mi'),'Yyyy.Mm.Dd Hh24:Mi:ss') Hhmm From Dual;
HH                  HHMM
------------------- -------------------
2004.05.09 11:00:00 2004.05.09 11:17:00
 
42.CHARTOROWID
将字符数据类型转换为ROWID类型
SQL> Select Rowid,Rowidtochar(Rowid),Ename From Scott.Emp;
ROWID              ROWIDTOCHAR(ROWID) ENAME
------------------ ------------------ ----------
AAAAfKAACAAAAEqAAA AAAAfKAACAAAAEqAAA SMITH
AAAAfKAACAAAAEqAAB AAAAfKAACAAAAEqAAB ALLEN
AAAAfKAACAAAAEqAAC AAAAfKAACAAAAEqAAC WARD
AAAAfKAACAAAAEqAAD AAAAfKAACAAAAEqAAD JONES

43.CONVERT(C,Dset,Sset)
将源字符串 Sset从一个语言字符集转换到另一个目的Dset字符集
SQL> Select Convert('Strutz','We8hp','F7dec') "Conversion" From Dual;
Conver
------
Strutz

44.HEXTORAW
将一个十六进制构成的字符串转换为二进制

45.RAWTOHEXT
将一个二进制构成的字符串转换为十六进制
 
46.ROWIDTOCHAR
将ROWID数据类型转换为字符类型
 
47.TO_CHAR(Date,'Format')
SQL> Select To_char(Sysdate,'Yyyy/Mm/Dd Hh24:Mi:ss') From Dual;
TO_CHAR(SYSDATE,'YY
-------------------
2004/05/09 21:14:41
 
48.TO_DATE(String,'Format')
将字符串转化为ORACLE中的一个日期

49.TO_MULTI_BYTE
将字符串中的单字节字符转化为多字节字符
SQL>  Select To_multi_byte('高') From Dual;
TO
--

50.TO_NUMBER
将给出的字符转换为数字
SQL> Select To_number('1999') Year From Dual;
     YEAR
---------
     1999

linux

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