Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 330
  • Last Modified:

SQL parameter for Previous Month's Date

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
GPSPOW
Asked:
GPSPOW
  • 3
  • 3
  • 2
1 Solution
 
QuinnDexCommented:
this will give you previous month based on current date

dateadd(m,-1,getdate())

Open in new window

0
 
GPSPOWAuthor Commented:
I tried this one before and I do not get any data back.

Glen
0
 
QuinnDexCommented:
past your current query in and ill have a look
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
Simone BCommented:
WHERE (YEAR(DATEADD(m,-1,GETDATE())) = YEAR(PeriodDateTime)
AND MONTH(DATEADD(m,-1,GETDATE())) = MONTH(PeriodDateTime))
0
 
Simone BCommented:
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
 
GPSPOWAuthor Commented:
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
 
GPSPOWAuthor Commented:
Thank you that worked perfecty.

Glen
0
 
QuinnDexCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now