zoukankan      html  css  js  c++  java
  • SQL >日期函数

    执行实例(表:life_unite_product 有createtime时间字段)
    select day(createtime) from life_unite_product     --取时间字段的天值
    select month(createtime) from life_unite_product   --取时间字段的月值
    select year(createtime) from life_unite_product    --取时间字段的年值
    select datepart(yy,createtime) from life_unite_product     --取时间字段的年值
    select datepart(qq,createtime) from life_unite_product     --取时间字段的季度值
    select datepart(mm,createtime) from life_unite_product     --取时间字段的月值
    select datepart(dy,createtime) from life_unite_product     --取时间字段是那年的第几天
    select datepart(dd,createtime) from life_unite_product     --取时间字段的天值
    select datepart(wk,createtime) from life_unite_product       --取时间字段是那年的第几个星期
    select datepart(dw,createtime) from life_unite_product       --取时间字段是那年的那个星期的第几个工作日(工作日从星期日开算)
    select datepart(hh,createtime) from life_unite_product     --取时间字段的小时值
    select datepart(mi,createtime) from life_unite_product     --取时间字段的分钟值
    select datepart(ss,createtime) from life_unite_product     --取时间字段的秒值
    select datepart(ms,createtime) from life_unite_product     --取时间字段的毫秒值
    select dateadd(yy,-1,createtime) from life_unite_product   ----取时间字段(年份被减1了)
    select dateadd(mm,3,createtime) from life_unite_product   ----取时间字段(月份被加3了)
    select dateadd(dd,1,createtime) from life_unite_product   ----取时间字段(日被加1了)
    select DATEDIFF(yy,createtime,getdate()) from life_unite_product --与当前日期的年份差
    select DATEDIFF(mm,createtime,getdate()) from life_unite_product --与当前日期的月份差
    select DATEDIFF(dd,createtime,getdate()) from life_unite_product --与当前日期的日数差
    select DATEDIFF(mi,createtime,getdate()) from life_unite_product --与当前日期的分钟数差
    select datename(yy,createtime) from life_unite_product   --取时间字段的年值
    select datename(mm,createtime) from life_unite_product   --取时间字段的月值
    select datename(dd,createtime) from life_unite_product   --取时间字段的天值
    select getdate()   --取当前时间
    日期函数
    1、day(date_expression)
    返回date_expression中的日期值
    2、month(date_expression)
    返回date_expression中的月份值
    3、year(date_expression)
    返回date_expression中的年份值
    4、DATEADD()
    DATEADD (, , )
    返回指定日期date 加上指定的额外日期间隔number 产生的新日期。参数“datepart” 取值如下:
    5、DATEDIFF()
    DATEDIFF (, , )
    返回两个指定日期在datepart 方面的不同之处,即date2 超过date1的差距值,其结果值是一个带有正负号的整数值。
    6、DATENAME()
    DATENAME (, )
    以字符串的形式返回日期的指定部分此部分。由datepart 来指定。
    7、DATEPART()
    DATEPART ( datepart , date )
    以整数值的形式返回日期的指定部分。此部分由datepart 来指定。
    DATEPART (dd, date) 等同于DAY (date)
    DATEPART (mm, date) 等同于MONTH (date)
    DATEPART (yy, date) 等同于YEAR (date)
    下表列出了 datepart 选项以及 SQL Server Compact Edition 所识别的缩写:
    日期部分        缩写
    年份            yy、yyyy
    季度            qq、q
    月份            mm、m
    每年的某一日    dy、y
    日期            dd、d
    星期            wk、ww
    工作日*         dw
    小时            hh
    分钟            mi、n
    秒              ss、s
    毫秒            ms
    8、GETDATE()
    以DATETIME 的缺省格式返回系统当前的日期和时间
  • 相关阅读:
    代码演示C#各版本新功能
    【转】Spring Security Authentication (认证)
    maven groupId分组名称,artifactId项目名称
    【转】Maven的本地仓库和镜像源配置
    【转】asp.net core环境变量详解
    【转】建议收藏备用:.net core使用QRCoder生成普通二维码和带Logo的二维码详细使用教程,源码已更新至开源模板
    【转】VS中添加自定义代码片段
    【转】Java JDK和IntelliJ IDEA 配置及安装
    Download .NET Core
    站点部署,IIS配置优化指南[转]
  • 原文地址:https://www.cnblogs.com/yjung/p/1652650.html
Copyright © 2011-2022 走看看