Solved

How do I query a date range from beginning to end of the previous month in YYYYMMDD format?

Posted on 2011-03-17
5
574 Views
Last Modified: 2012-05-11
I currently have a large amount of SQL queries built for the purpose of Month End reporting.  A common factor in these queries is retrieving total counts in tables between the first and last days of the previous month.  The field in question is BookDate, which is formatted as YYYYMMDD.  

Example:
Say today is March 1st, 2011 (20110301) and I want a total count of transactions in the ActivityHist table that are cash from February 1st, 2011 (20110201) through February 28th (20110228)... (See Attached Code)

Currently, I manually change all BookDate info to reflect the previous month... How can I do this automatically?

Regards,

NCollinsBBP


SELECT COUNT(*)
FROM ActivityHist
WHERE CashTran = '1' AND
      BookDate >= '20110201' AND BookDate <= '20110228'

Open in new window

0
Comment
Question by:NCollinsBBP
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:lludden
ID: 35158524
To get the first and last day of the previous month,

SELECT DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)) AS LastDayPrevMonth,
      CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(GETDATE())-1),CAST(GETDATE() as date)),101) AS FirstDayPrevMonth
0
 
LVL 18

Expert Comment

by:lludden
ID: 35158570
If you need this formatted as a string, then use CONVERT to change to YYYYMMDD format,

SELECT CAST(varchar(8),getdate(),112)

0
 

Author Comment

by:NCollinsBBP
ID: 35159056
If you don't mind, how exactly can I incorporate that into the query I attached?  The BookDate field in my database is plain text and not a datetime field....
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 250 total points
ID: 35159420
Try:

SELECT COUNT(*)
FROM ActivityHist
WHERE CashTran = '1' AND
      left(BookDate,6) = left(convert(varchar(8),dateadd(mm,-1,getdate()),112),6)

Open in new window


I hope this helps.
0
 

Author Closing Comment

by:NCollinsBBP
ID: 35159503
Perfection!
This solution gave me the same exact result as my attached query from my original post.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

762 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

19 Experts available now in Live!

Get 1:1 Help Now