表A
id  name   time
1 '都是' 2010-03-22 00:42:47.293
2 '都是' 2010-04-22 00:43:33.187
3 '都是' 2010-04-23 00:43:37.280
4 '都是' 2010-05-22 00:43:39.450
5 '都是' 2010-06-22 00:43:40.543
6 '都是' 2010-07-22 00:43:41.560
7 '都是' 2011-04-22 00:43:47.107
8 '都是' 2012-04-22 00:43:50.293
9 '都是' 2013-04-22 00:43:59.950
10'都是' 2010-04-23 00:44:05.310
11'都是' 2010-04-22 00:44:07.170
12'都是' 2010-05-22 00:44:08.623怎么根据日期查询啊 查询2010年的所有Name
查询2010年3月所有数据
查询2010年3月22日的所有数据谢谢

解决方案 »

  1.   

    select * from tb where month(time)=4
    datepart
      

  2.   

    select name from 表A where time between '2010-1-1' and '2010-12-31' select * from 表A where time between '2010-3-1' and '2010-3-31'select * from 表A where time='2010-3-22'
      

  3.   

    1 select * from tb where time>=cast('2010' as datetime) and time<cast('2011' as datetime)
    2 select * from tb where time>=cast('2010-3' as datetime) and time<cast('2010-4' as datetime)
    3 select * from tb where time>=cast('2010-3-22' as datetime) and time<cast('2010-3-23' as datetime)
      

  4.   

    --查询2010年的所有Name
     
    select * from tb where year(time)=2010--查询2010年3月所有数据
    select * from tb where year(time)=2010 and month(time)=3
    --查询2010年3月22日的所有数据
    select * from tb where year(time)=2010 and month(time)=3 and day(time)=22
      

  5.   

    当然可以跟一楼的一样,用month和year函数进行查询
    select name from 表A where year(time)=2010select * from 表A where month(time)=3 select * from 表A where time='2010-3-22'
      

  6.   

    --查询2010年的所有Name
     
    select * from tb where year(time)=2010--查询2010年3月所有数据
    select * from tb where year(time)=2010 and month(time)=3
    --查询2010年3月22日的所有数据
    select * from tb where convert(CHAR(10),TIME,20)='2010-03-22'
      

  7.   

    已经说的很清楚了,有很多相关日期转换函数,Year(),Month(),Convert(),Cast().......,你可以查看SQL  server 帮助。
      

  8.   

    select name from tb where year(time)=2010
    select * from tb where datediff(mm,'2010-3-1',time)=0
    select * from tb where datediff(d,'21010-3-22'time)=0