Solved

SQL parameter for Previous Month's Date

Posted on 2013-12-20
8
293 Views
Last Modified: 2013-12-20
I am trying to get the correct syntax to filter my selections for the previous month's date based on today's date.

Attached is picture of the query code and the PeriodDateTime field (mm-dd-yyyy hh:mm)

I can hard code the month end to say 11/30/13 and no problem.  I just cannot get the correct DateAdd or DateDiff syntax to make it variable.

Thanks

Glen
select.jpg
0
Comment
Question by:GPSPOW
  • 3
  • 3
  • 2
8 Comments
 
LVL 9

Expert Comment

by:QuinnDex
Comment Utility
this will give you previous month based on current date

dateadd(m,-1,getdate())

Open in new window

0
 

Author Comment

by:GPSPOW
Comment Utility
I tried this one before and I do not get any data back.

Glen
0
 
LVL 9

Expert Comment

by:QuinnDex
Comment Utility
past your current query in and ill have a look
0
 
LVL 11

Accepted Solution

by:
Simone B earned 500 total points
Comment Utility
WHERE (YEAR(DATEADD(m,-1,GETDATE())) = YEAR(PeriodDateTime)
AND MONTH(DATEADD(m,-1,GETDATE())) = MONTH(PeriodDateTime))
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Expert Comment

by:Simone B
Comment Utility
You're not getting any data because your period dates are all on the last day of the month. When this is the case, you need to filter on the month and year only, ignoring the day and time.
0
 

Author Comment

by:GPSPOW
Comment Utility
SELECT TOP (100) percent [SourceID]
      ,[VisitID]
      ,[PeriodDateTime]
      ,[BillingID]
      ,[AgencyID]
      ,[AgencyName]
      ,[AnyClDisExemptions]
      ,[ArAgeDateTime]
      ,[ArChgTotal]
      ,[Balance]
      ,[BarStatus]
      ,[BdAgeDateTime]
      ,[BillRuleID]
      ,[ClPendingCharges]
      ,[ClUnappliedCredits]
      ,[ClientID]
      ,[Contract1stPmtDtDateTime]
      ,[ContractAmount]
      ,[ContractDateTime]
      ,[ContractPaid]
      ,[CorpID]
      ,[CorpName]
      ,[FeeScheduleID]
      ,[FeeScheduleName]
      ,[InsuranceBalance]
      ,[LastBillTxn]
      ,[LastPayDateTime]
      ,[LastPostTxn]
      ,[LastStTxn]
      ,[LastTxn]
      ,[PtBalance]
      ,[PtType]
      ,[StmtGrpID]
      ,[TypeID]
      ,[TypeName]
      ,[UrChgTotal]
      ,[ZeroDateTime]
      ,[RowUpdateDateTime]
      ,[FinalDateTime]
      ,[LastBilledDateTime]
      ,[LateChargeTotal]
      ,[ProfessionalChargeTotal]
  FROM [livedb].[dbo].[BarPeStatusVectors]
  where PeriodDateTime='11/30/13' --DATEADD(m,-1,getdate())
 
  and BarStatus in ('UB','FB','IB') and Balance <>0




I commented out your suggestion above.

Current syntax will produce data.

Glen
0
 

Author Closing Comment

by:GPSPOW
Comment Utility
Thank you that worked perfecty.

Glen
0
 
LVL 9

Expert Comment

by:QuinnDex
Comment Utility
i see whats happening that returns date and time, to use it in a query you need time to be 00:00:00

this will do that for you

DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), -1)

Open in new window

0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

744 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

16 Experts available now in Live!

Get 1:1 Help Now