select month 12

  • hi

    i'm sure this has been posted before but i'm not sure how to ask the questions so new topic...

    i want to run a query for dec sales figures. so something like...

    MyMonth = month(getdate()) -1

    OR

    datediff(mm, MyDate, Getdate()) Between 1 and 1

    neither seem to want to work for me though. Is there anything i can do to handle this without writing a function?

    thanks.

  • the best way is

    where DateCol >='20111201' and DateCol<'20120101'

    Dave



    Clear Sky SQL
    My Blog[/url]

  • thanks

    however, i need it to be dynamic so it runs on the first of every month. it's only an issue when its a new year.

  • So,

    Declare @FirstMnth datetime

    Select @FirstMnth = dateadd(mm,datediff(mm,0,getdate()),0)

    Select Id

    where DateCol >=@FirstMnth and DateCol < dateadd(mm,1,@Firstmnth)

    BTW this is good reading : http://www.karaszi.com/SQLServer/info_datetime.asp



    Clear Sky SQL
    My Blog[/url]

  • this works perfectly thanks

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply