Set time portion of a datetime variable

后端 未结 6 1042
慢半拍i
慢半拍i 2020-12-09 18:25

I am working on a query that will be an automated job. It needs to find all the transactions between 8 PM and 8 PM for the last day. I was thinking of doing something like

6条回答
  •  悲哀的现实
    2020-12-09 19:00

    I had to do something similar, create a procedure to run from a certain time the previous day to a certain time on the current day.

    This is what I did to set the start date to 16:30 on the previous day, basically subtract the parts you don't want to get them back to 0 then add the value that you want it to be.

    -- Set Start Date to previous day and set start time to 16:30.00.000
    
    SET @StartDate = GetDate()
    SET @StartDate = DateAdd(dd,- 1, @StartDate) 
    SET @StartDate = DateAdd(hh,- (DatePart(hh,@StartDate))+16, @StartDate) 
    SET @StartDate = DateAdd(mi,- (DatePart(mi,@StartDate))+30, @StartDate) 
    SET @StartDate = DateAdd(ss,- (DatePart(ss,@StartDate)), @StartDate) 
    SET @StartDate = DateAdd(ms,- (DatePart(ms,@StartDate)), @StartDate) 
    

    Hope this helps someone.

提交回复
热议问题