关于DateTime类型和Date类型比较时的困惑

 1 CREATE TABLE T_TEST(ID INT IDENTITY PRIMARY KEY,CreateTime DATE)

 2 INSERT INTO T_TEST SELECT '2011-11-04'

 3 UNION ALL SELECT '2011-11-04'

 4 UNION ALL SELECT '2011-11-04'

 5 UNION ALL SELECT '2011-11-05'

 6 UNION ALL SELECT '2011-11-05'

 7 UNION ALL SELECT '2011-11-05'

 8 

 9 SELECT * FROM T_TEST

10 

11 SELECT COUNT(*) FROM T_TEST 

12 WHERE  CreateTime>='2011-11-04 00:00:00' AND CreateTime <'2011-11-04 23:59:59'

13 --等价于(返回0行 不太明白????)

14 

15 --传入参数为datetime类型(返回3行,这个可以理解,但和上面又有啥区别呢?不解)

16 SP_executesql N'

17     select COUNT(*) 

18     from T_TEST

19     where CreateTime>=@BeginTime and CreateTime<@EndTime

20 ',N'@BeginTime datetime,@EndTime datetime',

21 @BeginTime='2011-11-04 00:00:00',

22 @EndTime='2011-11-04 23:59:59'    

23 

24 --修改了传参类型为date类型(返回0行,这个也可以理解)

25 SP_executesql N'

26     select COUNT(*) 

27     from T_TEST

28     where CreateTime>=@BeginTime and CreateTime<@EndTime

29 ',N'@BeginTime date,@EndTime date',

30 @BeginTime='2011-11-04 00:00:00',

31 @EndTime='2011-11-04 23:59:59'

32 

33 --又看下面的例子

34 DECLARE @EndTime datetime

35  SET @EndTime='2011-11-04 23:59:59' 

36 DECLARE @CreateTime DATE

37  SET @CreateTime='2011-11-04' 

38 

39 IF @CreateTime<@EndTime  --打印出来,可以理解,但为啥前面的不对呢?

40  PRINT '@CreateTime<@EndTime'

 

你可能感兴趣的:(DateTime)