Is there a simipler way of filtering by current month

  • If not I guess I can create my own udf. Thanks

    SELECT something

    FROM someentity

    WHERE DATEPART(MONTH, somedate) = DATEPART(MONTH, GETDATE())

    AND DATEPART(YEAR, somedate) = DATEPART(YEAR, GETDATE())

  • Here's how I'd do it:

    SELECT something

    FROM someentity

    WHERE somedate>= DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)

    Please note that I removed the datepart function from the source column to make it SARG-able (= to allow for an index seek instead of a scan assuming there's an index that could be used).



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • you are a genius.

    i forgot my code won't be using an index with that datepart.

    thanks!

  • SQL Iron Chef (1/26/2011)


    you are a genius.

    i forgot my code won't be using an index with that datepart.

    thanks!

    :blush:

    Naah... I'm just trying to return what I've learned over the years hanging around here. 😉

    So: Glad I could help 😀



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • For some other date manipulation functions, look here: Some Common Date Routines.

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

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