天天看點

SQL時間相關 - SQL日期,時間比較

SQL Server 中時間比較

例子:

select count(*) from table where DATEDIFF ([second], '2004-09-18 00:00:18', '2004-09-18 00:00:19')  > 0

說明

  select  DATEDIFF(day, time1 , time2)    對應示例語句如下

  select  DATEDIFF(day, '2010-07-23 0:41:18', '2010-07-23 23:41:18')

  time1 > time2 為負數;

  time1 < time2 為正數;

  [day] :隻會比較 2010-07-23 忽略 0:41:18' 其他同理

以下分别: 

  年: SELECT DATEDIFF([year],time1 , time2)   傳回值: -6 ,說明是後減前 與 mysql 相反的。

  月: SELECT DATEDIFF([month], time1 , time2)

  天: SELECT DATEDIFF([day], time1 , time2)

  小時: SELECT DATEDIFF([hour], time1 , time2)

  秒: SELECT DATEDIFF([second], time1 , time2) 

本文轉自左正部落格園部落格,原文連結:http://www.cnblogs.com/soundcode/p/4011391.html,如需轉載請自行聯系原作者