欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

sql截取日期/时间的单独部分,比如年、月、日、小时、分钟等等

程序员文章站 2022-04-28 21:36:53
可以使用EXTRACT() 函数。(oracle和mysql都有该函数)语法: EXTRACT(unit FROM date)date 参数是合法的日期表达式。unit 参数可以是下列的值:YEAR\MONTH\WEEK\DAY\HOUR\MINUTE\SECOND (这里只列出部分常用值)上面依次 ......
可以使用extract() 函数。(oracle和mysql都有该函数)

语法:
extract(unit from date)

date 参数是合法的日期表达式。unit 参数可以是下列的值:
year\month\week\day\hour\minute\second (这里只列出部分常用值)
上面依次代表:年、月、周、日、时、分、秒

示例:table 表id为1的数据有一个日期类型的字段 createdate 值为 2019-11-12 10:12:33

select
extract(year from createdate) as createyear,
extract(month from createdate) as createmonth,
extract(day from createdate) as createday from table where id = 1

结果:
createyear -- 2019
createmonth -- 11
createday  -- 12