Solved

SQL parameter for Previous Month's Date

Posted on 2013-12-20
8
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 9

Expert Comment

by:QuinnDex
ID: 39732275
this will give you previous month based on current date

dateadd(m,-1,getdate())

Open in new window

0
 

Author Comment

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

Glen
0
 
LVL 9

Expert Comment

by:QuinnDex
ID: 39732292
past your current query in and ill have a look
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 11

Accepted Solution

by:
Simone B earned 500 total points
ID: 39732296
WHERE (YEAR(DATEADD(m,-1,GETDATE())) = YEAR(PeriodDateTime)
AND MONTH(DATEADD(m,-1,GETDATE())) = MONTH(PeriodDateTime))
0
 
LVL 11

Expert Comment

by:Simone B
ID: 39732302
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
ID: 39732307
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
ID: 39732314
Thank you that worked perfecty.

Glen
0
 
LVL 9

Expert Comment

by:QuinnDex
ID: 39732316
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

734 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