簡體   English   中英

固定日期以提取上個月和當前月份數據

[英]Date fixing to pull previous month & current month data

我們的系統將數據拉到前一天,如果當前日期是第一個,如果當前日期是在第一個之后,我試圖查看上個月提取數據的選項,那么在這種情況下,我需要查看從當月開始到月末的數據

有了這個月,我不斷更改“@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)

我不想在這里每月進行人工干預。

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)

我想你想要這樣的東西:

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