日期函数 day() 、month()、year()
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 的缺省格式返回系统当前的日期和时间
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~