当前位置:首页 » 编程语言 » oracle里sql函数
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

oracle里sql函数

发布时间: 2022-10-16 11:07:25

① oracle_plsql函数大全

1.ascii('A') 返回与指定的字符对应的十进制数
2.chr(65) 给出整数,返回对应的字符;
3. concat('010-','88888888') 连接字符串
4.initcap('smith') 返回字符串并将字符串的第一个字母变为大写;
5.instr(C1,C2,I,J) 在字符串C1中搜索字符串C2,返回发现指定的字符的位置;
I搜索的开始位置,默认为1。 J出现的位置,默认为1。
6.length(name) 返回字符串的长度;
7. lower('AaBbCcDd') 返回字符串,并将所有的字符小写
8. upper('AaBbCcDd') upper from al;返回字符串,并将所有的字符大写
9.rpad和lpad(粘贴字符) lpad(rpad('gao',10,'*'),17,'*') 得*******gao*******
10.ltrim和rtrim和trim 删除字符两边出现的空格。
11. substr (string,start,count)取子字符串,从start开始,取count个
12. replace ('string','s1','s2') string希望被替换的字符或变量,s1被替换的字符串
s2要替换的字符串
13. soundex 返回一个与给定的字符串读音相同的字符串
14.TRIM('s' from 'string') 剪掉前面和后面的字符
15. abs(100) 返回指定值的绝对值
16. acos(-1) 给出反余弦的值
17. asin(0.5) 给出反正弦的值
18. atan(1) 返回一个数字的反正切值
19. ceil(3.1415927) 返回大于或等于给出数字的最小整数
20. cos(-3.1415927) 返回一个给定数字的余弦
21. cosh(20) 返回一个数字反余弦值
22. exp(2) 返回一个数字e的n次方根
23. floor(2345.67) 对给定的数字取整数
24. ln(1) 返回一个数字的对数值
25.log(n1,n2) 返回一个以n1为底n2的对数
26.mod(n1,n2) 返回一个n1除以n2的余数
27.power(n1,n2) 返回n1的n2次方根
28. round和trunc 按照指定的精度进行舍入
29.sign(n) 取数字n的符号,大于0返回1,小于0返回-1,等于0返回0
30. sin(1.57079) 返回一个数字的正弦值
31. sin(20) 返回双曲正弦的值
32.sqrt(n) 返回数字n的根
33. tan(n) 返回数字n的正切值
34. tanh(n) 返回数字n的双曲正切值
35. trunc (124.16666,2) 按照指定的精度截取一个数
36. add_months 增加或减去月份
37. last_day(sysdate) 返回日期的最后一天
38. months_between (date2,date1) 给出date2-date1的月份
39. new_time (date,'this','that') 给出在this时区=other时区的日期和时间
40. next_day (date,'day') 给出日期date和星期x之后计算下一个星期的日期
41. sysdate 用来得到系统的当前日期
42. chartorowid 将字符数据类型转换为ROWID类型
43. convert (c,dset,sset)
44. hextoraw 将一个十六进制构成的字符串转换为二进制
45. rawtohext 将一个二进制构成的字符串转换为十六进
46. rowidtochar 将ROWID数据类型转换为字符类型
47. to_char (date,'format')
48. to_date(string,'format') 将字符串转化为ORACLE中的一个日期
49. to_multi_byte('高') 将字符串中的单字节字符转化为多字节字符
50. to_number('1999') 将给出的字符转换为数字
51. bfilename (dir,file)
52. convert('x','desc','source') 将x字段或变量的源source转换为desc
53. mp(s,fmt,start,length)
54. empty_blob()和empty_clob() 这两个函数都是用来对大数据类型字段进行初始化操
作的函数
55. greatest('AA','AB','AC') 返回一组表达式中的最大值,即比较字符的编码大小.
56. least('啊','安','天') 返回一组表达式中的最小值
57. uid 返回标识当前用户的唯一整数(user_id=uid)
58. user 返回当前用户的名字
59. userevn 返回当前用户环境的信息,opt可以是:
ENTRYID,SESSIONID,TERMINAL,ISDBA,LABLE,LANGUAGE,CLIENT_INFO,LANG,VSIZE
ISDBA 查看当前用户是否是DBA如果是则返回true
60.avg(DISTINCT|ALL) all表示对所有的值求平均值,distinct只对不同的值求平均值
61.max(DISTINCT|ALL) 求最大值,DISTINCT表示对不同的值求最大值,相同的只取一次
62.min(DISTINCT|ALL) 求最小值,DISTINCT表示对不同的值求最小值,相同的只取一次
63. stddev (distinct|all) 求标准差,DISTINCT表示只对不同的值求标准差
64. variance (DISTINCT|ALL) 求协方差
65. group by主要用来对一组数进行统计
66. having 对分组统计再加限制条件
67.ORDER BY 用于对查询到的结果进行排序输出

② oracle 合并查询 事务 sql函数小知识学习

表查询:
合并查询:使用union关键字,可将满足条件的重复行去掉。
复制代码
代码如下:
select
ename,sal,job
from
emp
where
sal
>
2500
union
select
ename,sal,job
from
emp
where
job
=
'MANAGER';
而union
all用法和union相似,但是不会取消重复行。
intersect
用来取两个结果的交集。
minus用来取两个结果的差集。
使员工scott的岗位,工资,补助与SMITH员工一样。(使用子查询修改数据)
复制代码
代码如下:
update
emp
set
(job,sal,comm)=(select
job,sal,comm
from
emp
where
ename
=
'SMITH')
where
ename
=
'SCOTT';
事务:
设置保存点
savepoint
a
取消部分事务
roll
back
to
a
取消全部事务
rollback
设置为只读事务,用于统计某一刻之前的信息,而在统计过程中,可能还有访问,影响统计,所以,统计之前,设为只读事务,这样就保存此刻之前的结果,而之后的修改,将不会显示出来,设为只读事务的语句为:
复制代码
代码如下:
set
transaction
read
only;
设置之后会显示事务处理集。
sql函数:
将显示内容以小写形式显示,使用lower函数,比如
复制代码
代码如下:
select
lower(ename),sal
from
emp;
显示内容以大写形式显示,使用upper函数.还有length函数和substr函数。
复制代码
代码如下:
select
*
from
emp
where
length(ename)=5;
select
substr(ename,1,3)
from
emp;
substr表示从第一个取,取3个。
以首字母大写的方式显示所有员工的姓名。
将员工的姓名首字母大写
复制代码
代码如下:
select
upper(sub(ename,1,1)
from
emp;
将第一个字母之后的字母以小写的形式表示
复制代码
代码如下:
<PRE
class=sql
name="code">select
lower(substr(ename,2,length(ename)-1))
from
emp;</PRE><BR>
然后将两个结果合并,则得到了要显示的内容:
<PRE></PRE>
<PRE
class=sql
name="code"
sizcache="0"
sizset="11"><PRE
class=sql
name="code">select
upper(substr(ename,1,1))||lower(substr(ename,2,length(ename)-1))
as
name
from
emp;</PRE><BR>
替换函数replace
<PRE></PRE>
<PRE
class=sql
name="code"
sizcache="0"
sizset="14"><PRE
class=sql
name="code">select
replace(ename,'A','我')
from
emp;</PRE><BR>
<BR>
<BR>
<PRE></PRE>
</PRE></PRE>

③ oracle sql 管道函数是什么

oracle管道函数是一类特殊的函数,关键字PIPELINED表明这是一个oracle管道函数,oracle管道函数返回值类型必须为集合。
例子:
create or replace function f_pipeline_test
return MsgType
PIPELINED
as
begin
for i in 1 .. 10
loop
pipe row( 'Iteration ' || i || ' at ' || systimestamp );
dbms_lock.sleep(1);
end loop;
pipe row( 'All done!' );
return;
end;
/

④ oracle sql中count、case函数运用

count
表示的是计数,也就是说记录的条数,通常和分组函数一起使用。
sql:select
userId
,
count(*)
from
tablename
group
by
userId。
case表示的是多条件判断。
sql:select
ename,
case
when
sal<1000
then
'lower'
when
sal>1001
and
sal<2000
then
'modest'
when
sal>2001
and
sal<4000
then
'high'
else
'too
high'
end
from
emp;
以上语句就是一个简单的判断工资等级的一个case用法。

⑤ oracle sql中count、case函数运用

selecta.lastname,ISNULL(COUNT(1),0)
fromhrmresourcea,workflow_currentoperatorbwherea.id=b.useridgroupbya.lastnameorderby2desc

改成这个

⑥ oracle里的哪个函数等于sql的year函数急

sql中的year函数即取日期类型年份,在oracle中可用to_char函数。

sql中取当前时间的年份:

selectyear(getdate())

结果:

⑦ 求oracle sql函数清单以及使用说明

登录这个网站可以查询到你要的详细清单
docs.oracle.com

下面是部分函数清单及简要说明,仅供参考
Oracle SQL Functions
Functions:
ABS(n) Absolute value of number
ACOS(n) arc cosine of n
ADD_MONTHS(date,num_months)
Returns date + num_months
ASCII(char) Converts char into a decimal ascii code
ASIN(n) arc sine of n.
ATAN(n) arc tangent of n.
ATAN2(n.m) arc tangent of n and m.
AVG([DISTINCT]n)
Averge value of 'n' ignoring NULLs

BETWEEN value AND value
Where 'x' between 25 AND 100
BFILENAME('directory','filename')
Get the BFILE locator associated with a physical LOB binary file.

CASE Group the data into sub-sets.
CEIL(n) Round n up to next whole number.
CHARTOROWID(char) Converts a Char into a rowid value.
CHR(n) Character with value n
CONCAT(s1,s2) Concatenate string1 and string2
CONVERT(char_to_convert, new_char_set, old_char_set)
Convert a string from one character set to another.
COS(n) Cosine of number
COSH(n) Hyperbolic Cosine of number
COUNT(*) Count the no of rows returned
COUNT([DISTINCT] expr)
Count the no of rows returned by expr
CURRENT_DATE
CURRENT_TIME
CURRENT_TIMESTAMP

DECODE IF x THEN return y ELSE return z
DENSE_RANK Calculate the rank of a value in a group
DEREF(e) Return the object reference of argument e.
DUMP(expr,fmt[,start,length])
Convert to dec/hex/oct and display char set

EMPTY_BLOB Return an empty LOB locator (use to empty a column or variable)
EMPTY_CLOB Return an empty LOB locator (use to empty a column or variable)
EXISTS Return TRUE if a subquery returns at least one row
EXP(n) Exponential (e to 'n'th power)
EXTRACT Extract part of a DATE (Year,Month,Day,Second,etc)

FLOOR(n) Round n down to the next whole number.

GREATEST(expression, expression...)
Returns the largest in a list of expressions.
GROUPING Grouping for superaggregate rows=NULL
(see GROUP BY ROLLUP/CUBE)

HEXTORAW(char) Convert char containing hex digits to a raw value.

IN (list of comma separated values)
Effectively a shorthand for ['x' = y OR 'x' = z...] i.e.
Where 'x' IN ('sales','marketing','recruitment')
INITCAP(char) String with Initial Capitals
INSTR(str, chars[,s[,n]])
Find the 'n'th occurence of 'chars' in 'str'
Starting at position 's'
n and s default to 1
INSTRB (str, chars[,s[,n]])
The same as INSTR, except that 's' and the return value are expressed in bytes,
use for double-byte char sets.
IS NULL Check for NULL (empty) values (Select * from demo Where x IS NULL;)
IS NOT NULL Check for items that contain a value (Select * from demo Where x IS NOT NULL;)

LAST_DAY(date)Returns the last day of month in Date
LEAST(expression, expression...)
Returns the smallest in a list of expressions
LENGTH(char) Returns the number of characters in char
LENGTHB(char) Returns the number of bytes in char (use for double-byte char sets)
LIKE wildcard/value
Wildcards are [% = any chars] [ _ = any one char]
Where 'x' LIKE 'smith%' [will find 'Smithson']
Where 'x' LIKE 'smith_' [will find 'Smithy']
LN(n) Natural Log of n, where n>0
LOG(b,n) log of n, base b
LOWER(char) Returns character string in lowercase
LPAD(char, n[,PadChar])
Left Pad char with n spaces [or PadChars]
LTRIM(char[,set])
Left Trim char - remove leading spaces [or char set]

MAKE_REF(table,key)
Create a REF to a row of an OBJECT view/table
MAX([DISTINCT] expr)
Maximum value returned by expr
MIN([DISTINCT] expr)
Minimum value returned by expr
MOD(x,y) Remainder of x divided by y
MONTHS_BETWEEN(end_date, start_date)
Number of months between the 2 dates (integer)

NEW_TIME(date, zone1, zone2)
Convert between GMT and US time zones (but not CET)
NEXT_DAY(date,day_of_week)
'12-OCT-01','Monday' will return the next Mon after 12 Oct
NLS_CHARSET_DECL_LEN (bytecount,charset)
Returns the declaration width (no of chars) of an NCHAR column
NLS_CHARSET_ID(varchars)
Returns the char set ID given a charset name
NLS_CHARSET_NAME(charset_id)
Returns the char set name given a charset id
NLS_INITCAP(char[,'NLS_SORT = sort_sequence'])
Returns char in Initial Caps, using an NLS sort_sequence
either the session default or specified directly
NLS_LOWER(char[,'NLS_SORT = sort_sequence'])
Returns char in lower case, using an NLS sort_sequence
either the session default or specified directly
NLSSORT(char[,'NLS_SORT = sort_sequence'])
Return the string of bytes used to sort char, using an NLS sort_sequence
either the session default or specified directly
NLS_UPPER(char[,'NLS_SORT = sort_sequence'])
Returns char in UPPER case, using an NLS sort_sequence
either the session default or specified directly
NVL(expression, value_if_null)
If expression is null, returns value_if_null; if expression is not null, returns expression.
The arguments can have any datatype (Oracle will perform implicit conversion where needed).
PERCENT_RANK Calculate the percent rank of a value in a group.
POWER(m,n) m raised to the nth power

RANK Calculate the rank of a value in a group
RAWTOHEX(raw) Convert raw to a character value containing its hex equivalent
REF(table_alias)
Returns a REF value for an object instance (bound to the variable or row.)
The table alias (correlation variable) is associated with
one row of an object table or an object view in an SQL statement.
REFTOHEX(ref) Convert ref (object type) to a char value containing its hex equivalent.
REPLACE(char, search_str[, replace_str])
ANSI alternative to decode() Replace every occurrence of search_str
with replace_str, replace_str defaults to null.
ROUND(n,d) n rounded to d decimal places (d defaults to 0)
ROUND(date,fmt)
date rounded to fmt
ROWIDTOCHAR(rowid)
Convert a rowid value to VARCHAR2
ROW_NUMBER Assign a unique number to each row of results.
RPAD(char, n[,PadChar])
Right Pad char with n spaces [or PadChars]
RTRIM(char[,set])
Right Trim char - remove trailing spaces [or char set]

SIGN(n) positive = 1, zero = 0, negative = -1
SIN(n) Sine of n in Radians.
SINH(n) Hyperbolic Sine of n in Radians.
SOUNDEX(char) Returns a char value representing the sound of the words.
SQRT(n) Square Root (returns NULL for negative no's)
STDDEV([DISTINCT] n)
Standard deviation of n.
SUBSTR(char, s[,l])
A substring of char, starting at character s, length l.
SUBSTRB(char, s[,l])
A substring of char, starting at character s, length l
The same as SUBSTR, except that 's', 'l' and the return value are expressed in bytes,
use for double-byte char sets.
SUM([DISTINCT] n)
Sum of values of n, ignoring NULLs
SYS_CONTEXT('namespace','attribute_name')
Examine the package associated with the context namespace
Possible attributes are: NLS_TERRITORY, NLS_CURRENCY, NLS_CALENDAR
NLS_DATE_FORMAT, NLS_DATE_LANGUAGE, NLS_SORT, SESSION_USER, CURRENT_USER
CURRENT SCHEMAID,SESSION_USERID, CURRENT_USERID, CURRENT_SCHEMA
note: CURRENT_USER may be different from SESSION_USER within a stored procere
(e.g an invoker-rights procere).
SYS_CONTEXT ('USERENV','IP_ADDRESS')
SYS_GUID() Returns a globally unique identifier (16 byte RAW value)
SYSDATE The current system date & time
TAN(n) Tangent of n in Radians
TANH(n) Hyperbolic tangent of n in Radians
TO_BLOB(Raw_col) Convert LONG RAW and RAW values to BLOB
TO_CHAR Convert to a character String
TO_CLOB Convert character or NCLOB values to the database character set.
TO_DATE Convert to date value
TO_LOB(long) Convert LONG values to CLOB or NCLOB values
or convert LONG RAW values to BLOB values
TO_MULTI_BYTE(char)
Convert single-byte char to multi-byte char
TO_NCHAR(expr) Convert a TEXT expression, date, or number to NTEXT in a specified format.
Mostly used to format output data.
TO_NCLOB Convert any character string (including LOBs) to the national character set.
TO_NUMBER Convert to numeric format
TO_SINGLE_BYTE(char)
Convert multi-byte char to single-byte character.
TO_TIME Convert to time value
TO_TIME_TZ Convert to time zone
TO_TIMESTAMP Convert to timestamp
TO_TIMESTAMP_TZ
TO_YMINTERVAL Convert a character string to an INTERVAL YEAR TO MONTH type
TRANSLATE('char','search_str','replace_str')
Replace every occurrence of search_str with replace_str
unlike REPLACE() if replace_str is NULL the function returns NULL
TRANSLATE (text USING charset)
Convert text into a specific character set
Use this instead of CONVERT() if either the input or output datatype
is NCHAR or NVARCHAR2.
TRIM(LEADING|TRAILING|BOTH trim_char FROM trim_source)
Return trim_source as a VARCHAR2 with leading/trailing items removed
trim_char defaults to a space ' ' but may be numeric or char 'A'
TRUNC(i,d) Truncate i to d decimal places (d defaults to 0)
TRUNC(date,fmt) Truncate Date to nearest fmt.
UID User id (a unique number)
UPPER(char) Return characters in uppercase
USER Return the current Username
USERENV('option')
Can return any of the options: ENTRYID, SESSIONID,
TERMINAL, LANGUAGE, ISDBA, LANG, INSTANCE, CLIENT_INFO

VALUE(correlation_variable)
Return the object instance for a row of an object table
as associated with the correlation_variable (table alias)
VARIANCE([DISTINCT] n)
Variance of n, ignoring NULLs
VSIZE(expr) Value Size, returns the number of bytes used by each row of expr.

Examples
Return the left 4 characters from the column prod_code, like a left() function in other languages:
SQL> select substr(prod_code,1,4) from sales;
Return the right 3 characters from the column prod_code, like a right() function in other languages:
SQL> select substr(prod_code,-3) from sales;
Return the leftmost 2 digits of idnum:
SQL> select substr(to_char(idnum),1,2) from mytable;

This page is not an exhaustive list of all the functions available - to find a complete list of functions for a particular release of Oracle see docs.oracle.com or run this query:
SELECT distinct object_name
FROM all_arguments
WHERE package_name = 'STANDARD';

⑧ 求ORACLE SQL下的日期函数

1. 日期和字符转换函数用法(to_date,to_char)

select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from al; //日期转化为字符串
select to_char(sysdate,'yyyy') as nowYear from al; //获取时间的年
select to_char(sysdate,'mm') as nowMonth from al; //获取时间的月
select to_char(sysdate,'dd') as nowDay from al; //获取时间的日
select to_char(sysdate,'hh24') as nowHour from al; //获取时间的时
select to_char(sysdate,'mi') as nowMinute from al; //获取时间的分
select to_char(sysdate,'ss') as nowSecond from al; //获取时间的秒
select to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss') from al// 2.
select to_char( to_date(222,'J'),'Jsp') from al

显示Two Hundred Twenty-Two 3.求某天是星期几
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from al;
星期一
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from al;
monday
设置日期语言
ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';
也可以这样
TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American') 4. 两个日期间的天数
select floor(sysdate - to_date('20020405','yyyymmdd')) from al; 5. 时间为null的用法
select id, active_date from table1
UNION
select 1, TO_DATE(null) from al;

注意要用TO_DATE(null) 6.月份差
a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')
那么12月31号中午12点之后和12月1号的12点之前是不包含在这个范围之内的。
所以,当时间需要精确的时候,觉得to_char还是必要的

7. 日期格式冲突问题
输入的格式要看你安装的ORACLE字符集的类型, 比如: US7ASCII, date格式的类型就是: '01-Jan-01'
alter system set NLS_DATE_LANGUAGE = American
alter session set NLS_DATE_LANGUAGE = American
或者在to_date中写
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from al;
注意我这只是举了NLS_DATE_LANGUAGE,当然还有很多,
可查看
select * from nls_session_parameters
select * from V$NLS_PARAMETERS 8.
select count(*)
from ( select rownum-1 rnum
from all_objects
where rownum <= to_date('2002-02-28','yyyy-mm-dd') - to_date('2002-
02-01','yyyy-mm-dd')+1
)
where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' )
not in ( '1', '7' )

查找2002-02-28至2002-02-01间除星期一和七的天数
在前后分别调用DBMS_UTILITY.GET_TIME, 让后将结果相减(得到的是1/100秒, 而不是毫秒). 9. 查找月份
select months_between(to_date('01-31-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;
1
select months_between(to_date('02-01-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;
1.03225806451613

10. Next_day的用法
Next_day(date, day)

Monday-Sunday, for format code DAY
Mon-Sun, for format code DY
1-7, for format code D 11
select to_char(sysdate,'hh:mi:ss') TIME from all_objects
注意:第一条记录的TIME 与最后一行是一样的
可以建立一个函数来处理这个问题
create or replace function sys_date return date is
begin
return sysdate;
end;

select to_char(sys_date,'hh:mi:ss') from all_objects;

12.获得小时数
extract()找出日期或间隔值的字段值
SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 2:38:40') from offer
SQL> select sysdate ,to_char(sysdate,'hh') from al;

SYSDATE TO_CHAR(SYSDATE,'HH')
-------------------- ---------------------
2003-10-13 19:35:21 07

SQL> select sysdate ,to_char(sysdate,'hh24') from al;

SYSDATE TO_CHAR(SYSDATE,'HH24')
-------------------- -----------------------
2003-10-13 19:35:21 19
13.年月日的处理
select older_date,
newer_date,
years,
months,
abs(
trunc(
newer_date-
add_months( older_date,years*12+months )
)
) days

from ( select
trunc(months_between( newer_date, older_date )/12) YEARS,
mod(trunc(months_between( newer_date, older_date )),12 ) MONTHS,
newer_date,
older_date
from (
select hiredate older_date, add_months(hiredate,rownum)+rownum newer_date
from emp
)
) 14.处理月份天数不定的办法
select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'),last_day(sysdate) from al 16.找出今年的天数
select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from al 闰年的处理方法
to_char( last_day( to_date('02' | | :year,'mmyyyy') ), 'dd' )
如果是28就不是闰年 17.yyyy与rrrr的区别
'YYYY99 TO_C
------- ----
yyyy 99 0099
rrrr 99 1999
yyyy 01 0001
rrrr 01 2001 18.不同时区的处理
select to_char( NEW_TIME( sysdate, 'GMT','EST'), 'dd/mm/yyyy hh:mi:ss') ,sysdate
from al; 19.5秒钟一个间隔
Select TO_DATE(FLOOR(TO_CHAR(sysdate,'SSSSS')/300) * 300,'SSSSS') ,TO_CHAR(sysdate,'SSSSS')
from al 2002-11-1 9:55:00 35786
SSSSS表示5位秒数 20.一年的第几天
select TO_CHAR(SYSDATE,'DDD'),sysdate from al

310 2002-11-6 10:03:51 21.计算小时,分,秒,毫秒
select
Days,
A,
TRUNC(A*24) Hours,
TRUNC(A*24*60 - 60*TRUNC(A*24)) Minutes,
TRUNC(A*24*60*60 - 60*TRUNC(A*24*60)) Seconds,
TRUNC(A*24*60*60*100 - 100*TRUNC(A*24*60*60)) mSeconds
from
(
select
trunc(sysdate) Days,
sysdate - trunc(sysdate) A
from al
)
select * from tabname
order by decode(mode,'FIFO',1,-1)*to_char(rq,'yyyymmddhh24miss');

//
floor((date2-date1) /365) 作为年
floor((date2-date1, 365) /30) 作为月
d(mod(date2-date1, 365), 30)作为日. 23.next_day函数 返回下个星期的日期,day为1-7或星期日-星期六,1表示星期日
next_day(sysdate,6)是从当前开始下一个星期五。后面的数字是从星期日开始算起。
1 2 3 4 5 6 7
日 一 二 三 四 五 六

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

select (sysdate-to_date('2003-12-03 12:55:45','yyyy-mm-dd hh24:mi:ss'))*24*60*60 from dal
日期 返回的是天 然后 转换为ss

24,round[舍入到最接近的日期](day:舍入到最接近的星期日)
select sysdate S1,
round(sysdate) S2 ,
round(sysdate,'year') YEAR,
round(sysdate,'month') MONTH ,
round(sysdate,'day') DAY from al 25,trunc[截断到最接近的日期,单位为天] ,返回的是日期类型
select sysdate S1,
trunc(sysdate) S2, //返回当前日期,无时分秒
trunc(sysdate,'year') YEAR, //返回当前年的1月1日,无时分秒
trunc(sysdate,'month') MONTH , //返回当前月的1日,无时分秒
trunc(sysdate,'day') DAY //返回当前星期的星期天,无时分秒
from al 26,返回日期列表中最晚日期
select greatest('01-1月-04','04-1月-04','10-2月-04') from al 27.计算时间差
注:oracle时间差是以天数为单位,所以换算成年月,日

select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))/365) as spanYears from al //时间差-年
select ceil(moths_between(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanMonths from al //时间差-月
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanDays from al //时间差-天
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24) as spanHours from al //时间差-时
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60) as spanMinutes from al //时间差-分
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60*60) as spanSeconds from al //时间差-秒 28.更新时间
注:oracle时间加减是以天数为单位,设改变量为n,所以换算成年月,日
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n*365,'yyyy-mm-dd hh24:mi:ss') as newTime from al //改变时间-年
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),add_months(sysdate,n) as newTime from al //改变时间-月
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n,'yyyy-mm-dd hh24:mi:ss') as newTime from al //改变时间-日
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n/24,'yyyy-mm-dd hh24:mi:ss') as newTime from al //改变时间-时
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n/24/60,'yyyy-mm-dd hh24:mi:ss') as newTime from al //改变时间-分
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_char(sysdate+n/24/60/60,'yyyy-mm-dd hh24:mi:ss') as newTime from al //改变时间-秒 29.查找月的第一天,最后一天
SELECT Trunc(Trunc(SYSDATE, 'MONTH') - 1, 'MONTH') First_Day_Last_Month,
Trunc(SYSDATE, 'MONTH') - 1 / 86400 Last_Day_Last_Month,
Trunc(SYSDATE, 'MONTH') First_Day_Cur_Month,
LAST_DAY(Trunc(SYSDATE, 'MONTH')) + 1 - 1 / 86400 Last_Day_Cur_Month
FROM al;