Checking for a date that is within the prior month that I am running

Posted on 2011-10-14
Last Modified: 2012-05-12
I am running a report and I would like to check DATEA  is within the prior month that I am running?

Example:   If I run the report on October 9, I would like to select all records that DATEA is in September.

What would the syntax be in an MSSQL query using getdate() as today's date?
Question by:morinia
    1 Comment
    LVL 142

    Accepted Solution

    to get the start of this month:
    select convert(datetime, convert(varchar(8), getdate(), 120) + '01', 120)


    select * from yourtable
     where DATEA <=  convert(datetime, convert(varchar(8), getdate(), 120) + '01', 120)
       and DATEA > dateadd(month, -1,  convert(datetime, convert(varchar(8), getdate(), 120) + '01', 120))

    see also:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
    Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
    This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
    This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now