简体   繁体   中英

Date fixing to pull previous month & current month data

Our system pull data till previous day, I am trying to see options for pulling data for previous month if current date is 1st and if current date is after 1st then in that case I need to see data from current month beginning to end of month

With this on a monthly basis I keep changing the "@previous_calendar_date"

declare @first_day_of_previous_calendar_date_month datetime     
set @first_day_of_previous_calendar_date_month = dateadd(dd,-datepart(dd,@previous_calendar_date)+1,@previous_calendar_date)

I do not want manual intervention on a monthly basis here.

declare @previous_calendar_date datetime        
set @previous_calendar_date = '20190430'

declare @first_day_of_previous_calendar_date_month datetime     
set @first_day_of_previous_calendar_date_month = dateadd(dd,-datepart(dd,@previous_calendar_date)+1,@previous_calendar_date)

I think you want something like this:

where (day(getdate()) = 1 and datediff(month, datecol, getdate()) = 1) or
      (day(getdate()) <> 1 and datediff(month, datecol, getdate()) = 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM