Select records from current month

  • Hi,

    I use a very basic select query

    SELECT * FROM Client_Trades.DBO.Trades

    WHERE MONTH(Trade_Date) = '01'

    To extract records from a particular month. Is there any way of me not having to change the value e.g. '01' every month and just having it so it selects from the current month?

    Thanks for any help.

    EDIT: Trade_Date is a Date datatype.

  • Try this:

    DECLARE @Date DATE = GETDATE();

    SELECT *

    FROM Client_Trades.DBO.Trades

    WHERE Trade_Date >= DATEADD(MONTH, DATEDIFF(MONTH, 0, @Date), 0)

    AND Trade_Date < DATEADD(MONTH, DATEDIFF(MONTH, 0, @Date)+1, 0);

    What that does is calculate the first day of the current month, and the first day of the next month. Then it makes sure the Trade_Date >= first day of current, and < first day of next month.

    Does that do what you need?

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • This works perfectly. Thank you very much 🙂

Viewing 3 posts - 1 through 2 (of 2 total)

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