6

如何比较两个日期第一个列表一个 2013-04-04 05:47:52.000 第二个来自另一个表 2010-01-01 00:00:00.000。我只想比较年/月/日;如果它们相等,我会得到第二个 id 表。

4

3 回答 3

9

对于 Sql Server,您可以这样做:

CAST(table1date AS DATE) = CAST(table2date AS DATE)

如何使用它的示例:

declare @dateTime1 as datetime = '2013-04-04 05:47:52.000'
declare @dateTime2 as datetime = '2013-04-04 00:00:00.000'

if CAST(@dateTime1 AS DATE) = CAST(@dateTime2 AS DATE)
    print 'yy mm dd is the same'
else
    print 'not the same'

或使用表格:

declare @dateTime1 as datetime = '2013-04-04 05:47:52.000'
declare @dateTime2 as datetime = '2011-04-04 00:00:00.000'

declare @table1 table (id1 int, dt1 datetime)
declare @table2 table (id2 int, dt2 datetime, table1id int)
insert into @table1 values (1, @dateTime1)
insert into @table2 values (2, @dateTime2, 1)

select case when CAST(@dateTime1 AS DATE) = CAST(@dateTime2 AS DATE) then t2.id2 else t2.table1id end as id
from @table1 t1 join @table2 t2 on t1.id1 = t2.table1id
于 2013-07-22T23:33:36.140 回答
5
IF CAST(DateField1 AS DATE) = CAST(DateField2 AS DATE)
于 2013-07-23T03:53:05.357 回答
4

通过比较,您的意思是找出差异?

怎么样

DATEDIFF(datepart,startdate,enddate)

http://www.w3schools.com/sql/func_datediff.asp

于 2013-07-22T23:31:40.503 回答