简体   繁体   English

固定日期以提取上个月和当前月份数据

[英]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" 有了这个月,我不断更改“@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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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