Oracle日期函数总结
1.日期和字符转换函数用法:(to_char,to_date,to_namber)
使用to_date函数将字符转换成数字。
使用to_char函数对日期的转换,并也可以对数字转换。
使用to_namber函数将字符转换成日期
格式:必须包含在单引号中且大小写敏感;可以包含任意的有效的日期格式;日期之间用逗号隔开。
注意:(1)Dual 是一个‘伪表’作用于用来测试函数和表达式;(2)sysdate 格式化当前系统日期和时间
2.日期函数调试:
Months_between //两个日期相差的月数
代码:Select months_between(’01-9月-95’,’11-1月-94’) from employees
显示:19.67741…
Add_months //向指定日期中加上若干个月数
代码:Select months_between(’11-1月-94’,6) from employees
显示:11-07-94
Next_day //指定日期的下一个星期 对应的日期
代码:Select next_day(‘01-9月-95’,‘星期五’) from employees
显示:08-09-95
Last_day //本月的最后一天
代码:select last_day(‘01-2月-95’)from employees
显示:28-02-95
Rround //日期四舍五入
Select round(sysdate,‘year’)from employees
显示:01-10-19
Select round(sysdate,‘month)from employees
显示:01-04-9
Trunc //日期截断
Select trunc(sysdate,‘year’)from employees
显示:01-10-19
Select round(sysdate,‘month’)from employees
显示:01-04-9
3.系统的日期和时间格式
是指格式当前系统的日期和时间,并使用to_char数字转换函数。
代码:Select to_char(sysdate,yyyy-mm-dd hh24:mi:ss) from dual;
注意:使用双引号向日期添加字符。
4.数字符号转换格式
To_char格式:9数字、0零、$美元符、L本地货币符号、.小数点、,千位符
5.字符隐式格式
若字符串中有特殊字符,例如 ’123456789’,则无法进行隐式转换,需要使用to_numbar()来完成
6.查询相关的日期
对于把日期作为查询条件的查询,一般都使用to_date() 把一个字符串转为日期,这样可以不必关注日期格式