如何比较SQL Server 2008中的日期

时间:2014-08-29 05:04:45

标签: sql sql-server tsql

这里我需要使用SQL Server 2008来比较日期。如果特定日期出现多次,我想要显示该表的详细信息。

示例:

create table datetest
(
  columnx nvarchar(10),
  columny nvarchar(10),
  coldate date,
  coltime datetime
)

insert into datetest values('a123','b123','2014-01-01','01:05:00.000');
insert into datetest values('a123','b456','2014-01-01','02:15:00.000');
insert into datetest values('a123','b789','2014-01-01','03:25:00.000');
insert into datetest values('a321','b321','2014-02-03','10:05:00.000');
insert into datetest values('a243','b243','2014-03-04','11:05:00.000');
insert into datetest values('a243','b243','2014-03-04','12:05:00.000');
insert into datetest values('a243','b243','2014-03-04','12:05:00.000');

现在我只需要显示表中显示日期大于1的记录。

预期结果:

columnx    columny   coldate         coltime 
-------------------------------------------------------
a123        b123    2014-01-01  1900-01-01 01:05:00.000
a123        b456    2014-01-01  1900-01-01 02:15:00.000
a123        b789    2014-01-01  1900-01-01 03:25:00.000
a243        b243    2014-03-04  1900-01-01 11:05:00.000
a243        b243    2014-03-04  1900-01-01 12:05:00.000
a243        b243    2014-03-04  1900-01-01 12:05:00.000

我的尝试:

select * 
from datetest 
where coldate = '2014-01-01' or coldate = '2014-03-04';

注意:但这不是大型记录检查的正确方法。

1 个答案:

答案 0 :(得分:3)

试试这个

select * from datetest
where coldate in (select coldate from 
datetest group by coldate having count(*) > 1)

DEMO