牛骨文教育服务平台(让学习变的简单)
博文笔记

如何获取sql数据中时间的月份、年份(类型date)

创建时间:2017-08-31 投稿人: 浏览次数:5013

可用自带函数month来实现。
如:
创建表及插入数据:

create table test
(id int,
begindate datetime)

insert into test values (1,"2015-01-01")
insert into test values (2,"2015-02-01")

执行sql语句,获取月份:

select id,MONTH(begindate) as month from test

用mysql数据库
执行sql语句,获取年份:

select id,YEAR(begindate) as year from test

用mysql数据库

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。