我想实现一个文章归档功能,就是比如2012年2月的文章点进去就从数据库读取相应的文章列表,我现在数据的类型是DATETIME的就是年月日分秒,我只想读取几月份的这个代码怎么写?

解决方案 »

  1.   

    SQL语句,SELECT DATEPART(month, GETDATE())//读取今天的月份
      

  2.   

    SELECT * from table where DATEPART(month, 你的时间字段)<DATEPART(month, GETDATE())
      

  3.   


    declare @t table(date datetime)
    insert into @t(date) select '2011-12-20 11:22:22' union all select '2012-03-11 08:24:30'
    select year(date) as [年份],month(date) as [月份],datepart(month,date) as [月份1],convert(nvarchar(7),date,23) as [年月]
    from @t where convert(nvarchar(7),date,23) > '2011-12'
      

  4.   


    建议不要在列上用奇怪的函数。
    比如需要查询2012年2月的文章那就是 时间 between 2012-2-1 0:0:0 and 2012-02-29 23:59:59