设置日期时间变量的时间部分 [英] Set time portion of a datetime variable

查看:240
本文介绍了设置日期时间变量的时间部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个将是一个自动化工作的查询。最后一天需要在8点到8点之间找到所有交易。我正在想做这样的事情

  DECLARE @start_date DATETIME 
DECLARE @end_date DATETIME

SET @start_date = DATEADD(DAY,-2,GETDATE())
SET @end_date = DATEADD(DAY,-1,GETDATE())

对于自动查询,这有助于找出日期部分。但是变量的TIME部分是查询执行的当前时间。有没有一个快速简单的方法来硬编码两个变量的时间部分是8:00 PM?

解决方案

  DECLARE @start_date DATETIME 
DECLARE @end_date DATETIME

SET @start_date = DATEADD(小时,20,DATEDIFF(DAY,2,GETDATE()))
SET @end_date = @start_date + 1

选择@start_date,@end_date


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 this

DECLARE @start_date DATETIME
DECLARE @end_date DATETIME

SET @start_date = DATEADD(DAY, -2, GETDATE())
SET @end_date = DATEADD(DAY, -1, GETDATE())

For an automated query this works good at figuring out the date portion. But the TIME portion of the variable is the current time that the query executes. Is there a quick simple way to hard code the time portion of both variables to be 8:00 PM?

解决方案

DECLARE @start_date DATETIME
DECLARE @end_date DATETIME

SET @start_date = DATEADD(hour, 20, DATEDIFF(DAY, 2, GETDATE()))
SET @end_date = @start_date + 1

select @start_date, @end_date

这篇关于设置日期时间变量的时间部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆