现在的位置: 首页 > 综合 > 正文

SQL-Server中datepart函数的使用

2012年09月08日 ⁄ 综合 ⁄ 共 398字 ⁄ 字号 评论关闭

该函数是用来提取年份,月份,日期的一个函数,带两参数,第一个为(yy,mm,dd)其中一个,表示年月日,第二个参数为字段名称

比如提取某一年的记录

select *  from Employee  e where  datepart(yy,e.LastLoginTime)=2009

返回2009年所有记录

select *  from Employee  e where  datepart(mm,e.LastLoginTime)=12

返回12月份的所有记录

select *  from Employee  e where  datepart(dd,e.LastLoginTime)=30

返回30号所有记录

SELECT * FROM Employee e  WHERE datepart(mm,e.LastLoginTime)=12  AND  datepart(yy,e.LastLoginTime)=2009

返回09年12月份的记录

性能上没有做测试。

抱歉!评论已关闭.