码迷,mamicode.com
首页 > 数据库 > 详细

SQL 判断时间条件

时间:2015-07-10 18:37:54      阅读:389      评论:0      收藏:0      [点我收藏+]

标签:

1.判断年月日是否相等

select * from table 
where cast(convert(varchar(10), 时间字段, 120) as datetime)=‘2010-10-01‘

判断逻辑:通过convert函数现将时间字段中年月日截取出来,然后利用cast函数转化为datetime。

2.判读度分秒是否相等

set @transfrom_date= CAST(‘1900-1-1 ‘+ CONVERT(varchar(100), ‘时间字段‘, 108) as datetime)

select * from table 
where  CAST(‘1900-1-1 ‘+ CONVERT(varchar(100), ‘时间字段‘, 108) as datetime)=@transfrom_date

判断逻辑:现将时间条件语句转化为度分秒,然后执行查询并将比对的时间字段转化为同一年月日的度分秒。

3.判断是否为同一年份、月份、日

select *  from table 
--年份
where datediff(year,‘时间字段‘,‘时间条件‘)=0
--月份
where datediff(month,‘时间字段‘,‘时间条件‘)=0
--日 where datediff(day,‘时间字段‘,‘时间条件‘)=0
 


 

SQL 判断时间条件

标签:

原文地址:http://www.cnblogs.com/hjxh/p/4636383.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!