数据库之常用函数 (日期函数)

一、取得当前系统的时间

        oracle:select sysdate     //查询年月日时分秒
                      select current_date    //查询年月日时分秒
        mysql:select sysdate()         //查询年月日时分秒
                    select current_date    //查询年月日
                    select current_time   //查询时分秒
二、对日期值进行加减运算
        oracle:可以直接用加减符号对年进行加减
                        add_month(date,m)    //对月份进行加减,m为正的时候为加,为负时为减
        mysql:date_add(date,interval expression type)   //date为日期,type为日期类型    //加
                        type:year,month,quarter,week,day,hour,minute,second,miscosecond(微秒)
                                  year-month       (expression形式为yy-mm)
                                   day-hour,          (expression形式为dd:HH)
                                   day-minute,      (expression形式为dd:hh:mi)
                                   day-second       (expression形式为dd:hh:mi:ss)
                        example:date_add(now(),interval 1 year)
                    date_sub(date,interval expression type)               //减
三、取得日期之后指定工作日对应的日期(oracle)
        next_day(date,n)
        首先,在默认日期语言为英文的时候
        next_day(sysdate,'monday')
        若要为中文
        alter session set nls_date_language='simplified chinese'
        此时
        next_day(sysdate,'星期一')
四、取得日期之中的指定内容
        oracle:1.extract(date from datetime)     //取得年月日
                        select(year from sysdate)
                        extract(date from timestamp datatime)    //取得时分秒
                      2.to_char(date,[,fmt])
                        to_char(sysdate,'yyyy')
        mysql:    1.dayofyear()           //返回一年中某一天
                          dayname()        //返回实际中的某一天
                       2.dayofmonth()       //返回一月中某一天
                       3.dayofweek()       //返回一周的某一天(1-7,1为周末)
--------------------------------------------------------------------------------------------
                    year()       //返回年份
--------------------------------------------------------------------------------------------
                    month()      //返回月份数
                    monthname()    //返回月份名字
---------------------------------------------------------------------------------------------
                    weekofyear()      //返回日期对应的星期数
                    weekday    //返回星期值(0-6,0为周一)           
                   week()      //在一年中是第几个星期
                   week(date,first)
---------------------------------------------------------------------------------------------
                    hour(time)    //小时
                    minute()       //分钟
                    second()      //秒
五、取得指定日期所在月的最后一天
        last_day(date)                     //(oracle,mysql)
六、取得两个指定月份的差(oracle)
        months_between(date1,date2)
七、对日期时间舍入操作
        round(date,[,fmt])
        select round(sysdate,'yyyy')    //一年为单位舍入
八、截断指定日期(oracle)
        trunc(date [,fmt])
        select trunc(sysdate,'mm')    //一月份为单位截断
                              
posted @ 2013-09-09 14:20  SoulReaper  阅读(1454)  评论(0编辑  收藏  举报