熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Oracle >> 正文

ORACLE字符串處理函數(2)

2022-06-13   來源: Oracle 

  ADD_MONTHS

  增加或減去月份

  SQL> select to_char(add_months(to_date(yyyymm))yyyymm) from dual;

  TO_CHA

  

  

  SQL> select to_char(add_months(to_date(yyyymm))yyyymm) from dual;

  TO_CHA

  

  

  LAST_DAY

  返回日期的最後一天

  SQL> select to_char(sysdateyyyymmdd)to_char((sysdate)+yyyymmdd) from dual;

  TO_CHAR(SY TO_CHAR((S

  

  

  SQL> select last_day(sysdate) from dual;

  LAST_DAY(S

  

  

  MONTHS_BETWEEN(datedate)

  給出datedate的月份

  SQL> select months_between() mon_between from dual;

  MON_BETWEEN

  

  

  SQL>selectmonths_between(to_date(yyyymmdd)to_date(yyyymmdd)) mon_betw from dual;

  MON_BETW

  

  

  NEW_TIME(datethisthat)

  給出在this時區=other時區的日期和時間

  SQL> select to_char(sysdateyyyymmdd hh:mi:ss) bj_timeto_char(new_time

   (sysdatePDTGMT)yyyymmdd hh:mi:ss) los_angles from dual;

  BJ_TIME LOS_ANGLES

  

   :: ::

  NEXT_DAY(dateday)

  給出日期date和星期x之後計算下一個星期的日期

  SQL> select next_day(星期五) next_day from dual;

  NEXT_DAY

  

  

  SYSDATE

  用來得到系統的當前日期

  SQL> select to_char(sysdateddmmyyyy day) from dual;

  TO_CHAR(SYSDATE

  

   星期日

  trunc(datefmt)按照給出的要求將日期截斷如果fmt=mi表示保留分截斷秒

  SQL> select to_char(trunc(sysdatehh)yyyymmdd hh:mi:ss) hh

   to_char(trunc(sysdatemi)yyyymmdd hh:mi:ss) hhmm from dual;

  HH HHMM

  

   :: ::

  CHARTOROWID

  將字符數據類型轉換為ROWID類型

  SQL> select rowidrowidtochar(rowid)ename from scottemp;

  ROWID ROWIDTOCHAR(ROWID) ENAME

  

  AAAAfKAACAAAAEqAAA AAAAfKAACAAAAEqAAA SMITH

  AAAAfKAACAAAAEqAAB AAAAfKAACAAAAEqAAB ALLEN

  AAAAfKAACAAAAEqAAC AAAAfKAACAAAAEqAAC WARD

  AAAAfKAACAAAAEqAAD AAAAfKAACAAAAEqAAD JONES

  CONVERT(cdsetsset)

  將源字符串 sset從一個語言字符集轉換到另一個目的dset字符集

  SQL> select convert(strutzwehpfdec) conversion from dual;

  conver

  

  strutz

  HEXTORAW

  將一個十六進制構成的字符串轉換為二進制

  RAWTOHEXT

  將一個二進制構成的字符串轉換為十六進制

  ROWIDTOCHAR

  將ROWID數據類型轉換為字符類型

  TO_CHAR(dateformat)

  SQL> select to_char(sysdateyyyy/mm/dd hh:mi:ss) from dual;

  TO_CHAR(SYSDATEYY

  

  // ::

  TO_DATE(stringformat)

  將字符串轉化為ORACLE中的一個日期

  TO_MULTI_BYTE

  將字符串中的單字節字符轉化為多字節字符

  SQL> select to_multi_byte() from dual;

  TO

  

  高

  TO_NUMBER

  將給出的字符轉換為數字

  SQL> select to_number() year from dual;

  YEAR

  

  

  BFILENAME(dirfile)

  指定一個外部二進制文件

  SQL>insert into file_tb values(bfilename(lob_dirimagegif));

  CONVERT(xdescsource)

  將x字段或變量的源source轉換為desc

  SQL> select sidserial#usernamedecode(command

   none

   insert

  

   select

   update

   delete

   drop

   other) cmd from v$session where type!=background;

  SID SERIAL# USERNAME CMD

  

   none

   none

   none

   none

   none

   none

   none

   none

   GAO select

   GAO none

  DUMP(sfmtstartlength)

  DUMP函數以fmt指定的內部數字格式返回一個VARCHAR類型的值

  SQL> col global_name for a

  SQL> col dump_string for a

  SQL> set lin

  SQL> select global_namedump(global_name) dump_string from global_name;

  GLOBAL_NAME DUMP_STRING

  

  ORACLEWORLD Typ= Len= CharacterSet=ZHSGBK: WORLD

  EMPTY_BLOB()和EMPTY_CLOB()

  這兩個函數都是用來對大數據類型字段進行初始化操作的函數

  GREATEST

  返回一組表達式中的最大值即比較字符的編碼大小

  SQL> select greatest(AAABAC) from dual;

  GR

  

  AC

  SQL> select greatest() from dual;

  GR

  

  天

  LEAST

  返回一組表達式中的最小值

  SQL> select least() from dual;

  LE

  

  啊

  UID

  返回標識當前用戶的唯一整數

  SQL> show user

  USER 為GAO

  SQL> select usernameuser_id from dba_users where user_id=uid;

  USERNAME USER_ID

  

  GAO

  USER

  返回當前用戶的名字

  SQL> select user from dual;

  USER

  

  GAO

  USEREVN

  返回當前用戶環境的信息opt可以是:

  ENTRYIDSESSIONIDTERMINALISDBALABLELANGUAGECLIENT_INFOLANGVSIZE

  ISDBA 查看當前用戶是否是DBA如果是則返回true

  SQL> select userenv(isdba) from dual;

  USEREN

  

  FALSE

  SQL> select userenv(isdba) from dual;

  USEREN

  

  TRUE

  SESSION

  返回會話標志

  SQL> select userenv(sessionid) from dual;

  USERENV(SESSIONID)

  

  

  ENTRYID

  返回會話人口標志

  SQL> select userenv(entryid) from dual;

  USERENV(ENTRYID)

  

  

  INSTANCE

  返回當前INSTANCE的標志

  SQL> select userenv(instance) from dual;

  USERENV(INSTANCE)

  

  

  LANGUAGE

  返回當前環境變量

  SQL> select userenv(language) from dual;

  USERENV(LANGUAGE)

  

  SIMPLIFIED CHINESE_CHINAZHSGBK

  LANG

  返回當前環境的語言的縮寫

  SQL> select userenv(lang) from dual;

  USERENV(LANG)

  

  ZHS

  TERMINAL

  返回用戶的終端或機器的標志

  SQL> select userenv(terminal) from dual;

  USERENV(TERMINA

  

  GAO

  VSIZE(X)

  返回X的大小(字節)數

  SQL> select vsize(user)user from dual;

  VSIZE(USER) USER

  

   SYSTEM

  AVG(DISTINCT|ALL)

  all表示對所有的值求平均值distinct只對不同的值求平均值

  SQLWKS> create table table(xm varchar()sal number());

  語句已處理

  SQLWKS> insert into table values(gao);

  SQLWKS> insert into table values(gao);

  SQLWKS> insert into table values(zhu);

  SQLWKS> commit;

  SQL> select avg(distinct sal) from gaotable;

  AVG(DISTINCTSAL)

  

  

  SQL> select avg(all sal) from gaotable;

  AVG(ALLSAL)

  

  

  MAX(DISTINCT|ALL)

  求最大值ALL表示對所有的值求最大值DISTINCT表示對不同的值求最大值相同的只取一次

  SQL> select max(distinct sal) from scottemp;

  MAX(DISTINCTSAL)

  

  

  MIN(DISTINCT|ALL)

  求最小值ALL表示對所有的值求最小值DISTINCT表示對不同的值求最小值相同的只取一次

  SQL> select min(all sal) from gaotable;

  MIN(ALLSAL)

  

  

  STDDEV(distinct|all)

  求標准差ALL表示對所有的值求標准差DISTINCT表示只對不同的值求標准差

  SQL> select stddev(sal) from scottemp;

  STDDEV(SAL)

  

  

  SQL> select stddev(distinct sal) from scottemp;

  STDDEV(DISTINCTSAL)

  

  

  VARIANCE(DISTINCT|ALL)

  求協方差

  SQL> select variance(sal) from scottemp;

  VARIANCE(SAL)

  

  

  GROUP BY

  主要用來對一組數進行統計

  SQL> select deptnocount(*)sum(sal) from scottemp group by deptno;

  DEPTNO COUNT(*) SUM(SAL)

  

  

  

  

  HAVING

  對分組統計再加限制條件

  SQL> select deptnocount(*)sum(sal) from scottemp group by deptno having count(*)>=;

  DEPTNO COUNT(*) SUM(SAL)

  

  

  

  SQL> select deptnocount(*)sum(sal) from scottemp having count(*)>= group by deptno ;

  DEPTNO COUNT(*) SUM(SAL)

  

  

  

  ORDER BY

  用於對查詢到的結果進行排序輸出

  SQL> select deptnoenamesal from scottemp order by deptnosal desc;

  DEPTNO ENAME SAL

  

   KING

   CLARK

   MILLER

   SCOTT

   FORD

   JONES

   ADAMS

   SMITH

   BLAKE

   ALLEN

   TURNER

   WARD

   MARTIN

   JAMES


From:http://tw.wingwit.com/Article/program/Oracle/201311/16630.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.