Solved

SQL parameter for Previous Month's Date

Posted on 2013-12-20
8
304 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
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

830 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