計算前一天,上乙個月

2021-05-22 16:42:26 字數 1748 閱讀 2439

最笨的方法

declare @timeend varchar(30),

@timestart varchar(30),

@subs varchar(2)

--計算統計的開始時間和結束時間

set @time=convert(varchar(30),@time,120)

--select @timestart=rtrim(ltrim(remark)) from dbo.baseinfo where baseinfoid=139--獲取分隔時間

select @timeend=rtrim(ltrim(remark)) from dbo.baseinfo where baseinfoid=140

set @subs=month(@time)--獲取月份

if(len(@subs)=1)

set @timeend=cast(year(@time) as varchar(4))+'-0'+@subs+'-'+@timeend

else

set @timeend=cast(year(@time) as varchar(4))+'-'+@subs+'-'+@timeend

set @subs=cast(cast(@subs as int)-1 as varchar(2))

if(len(@subs)=1)

set @timestart=cast(year(@time) as varchar(4))+'-0'+@subs+'-'+@timestart

else

set @timestart=cast(year(@time) as varchar(4))+'-'+@subs+'-'+@timestart

--select @timestart,@timeend

簡化:declare

@timestart varchar(30),

@timeend varchar(30),

@time varchar(30),

@detpid varchar(36)

set @detpid='43b5d11c-e8be-4cfd-ac99-ddff342ca7ad'

set @time='2010-06-02'

select @timeend=rtrim(ltrim(remark)) from dbo.baseinfo where baseinfoid=140

set @timeend=replace(@time,substring(@time,9,2),@timeend)

set @timestart=convert(varchar(30),dateadd(day,1,dateadd(month, -1, convert(datetime,@timeend,120))),120)

select cast(sum(convert(float,isnull(scores,100))/100*convert(float,remark))/sum(convert(float,remark)*convert(float,isnull(complete,1)))*100 as decimal(9,4)) as source

from view_getworkscore

where timeend between @timestart and @timeend and detpid=@detpid

select dateadd(month, -1, getdate())--上乙個月

select dateadd(day, -1, getdate()) --前一天

select getdate()

取前一天或後一天 乙個月時間

時間操作參考鳥哥部落格,令人困惑的strtotime。date 格式化時間返回string型別。date y m d h i s 獲得當前時間 current date date y m d time weeklater date y m d strtotime current date 1 wee...

PHP 取前一天或後一天 乙個月時間

獲得當前時間 date 格式化時間返回string型別。date y m d h i s current date date y m d time 根據當前時間加一周後 weeklater date y m d strtotime current date 1 week echo weeklate ...

strtotime 獲取上乙個月

1.案例 strtotime結合 1 month,1 month,next month 有時候會出現錯誤 date y m d strtotime 1 month 輸出 2018 07 01 1 month 程式設定 30天,相當於7月31日 30天得7月30號 var dump date y m ...