使用查询 SQL Server 2008 选择 DATEADD 分钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8988815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select DATEADD minutes with query SQL Server 2008
提问by soamazing
How can I add minutes using dateadd()
with a query result ?
如何使用dateadd()
查询结果添加分钟数?
Like this:
像这样:
SELECT dateadd(minute, (SELECT
isnull(time1 - time2, 0)
FROM table
WHERE field = 111) , getdate())
In this case, the select result is 288
but it don't add
在这种情况下,选择结果是288
但不添加
I need ADD this 288 in the function
我需要在函数中添加这个 288
Like this:
像这样:
SELECT dateadd(minute, 288, getdate())
回答by el ninho
SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, time1, time2), GETDATE())
FROM table
WHERE field = 111
This will add difference in minutes between time1 and time2 to current date, if that's what you want to achieve.
如果这是您想要实现的目标,这将增加 time1 和 time2 与当前日期之间的分钟差。
回答by Mike Dinescu
I think what you want is this query:
我想你想要的是这个查询:
SELECT DATEADD(minute, (ISNULL(time1 - time2, 0), GETDATE())
FROM table
WHERE field = 111
The above adds the difference between time1
and time2
(assuming time1 and time2 are some integer values) to the current date for each record in table where field
is 111. Note, however, that this DOES NOTupdate the records!
上面将time1
和time2
(假设 time1 和 time2 是一些整数值)之间的差异添加到表中每个记录的当前日期,其中field
是 111。但是请注意,这不会更新记录!
If you would like to update the records (i.e. modify the table as opposed to query it) then you need to use UPDATE
:
如果您想更新记录(即修改表而不是查询它),那么您需要使用UPDATE
:
UPDATE table
SET someField = dateadd(minute, (isnull(time1 - time2, 0), getdate())
WHERE field = 111
I hope this makes some sense. It wasn't very clear from your question what you were trying to do.
我希望这是有道理的。从你的问题中看你想做什么不是很清楚。
回答by Mike Dinescu
If your datatypes aren't correct, this might work for you:
如果您的数据类型不正确,这可能对您有用:
SELECT dateadd(minute, (SELECT
CAST(isnull(time1 - time2, 0) AS INT)
FROM table
WHERE field = 111) , getdate())
I'd still recommend you take a look at some of the other answers for a better structure - i.e. Miky Dinescus code above would be:
我仍然建议您查看其他一些答案以获得更好的结构 - 即上面的 Miky Dinescus 代码是:
SELECT DATEADD(minute, (CAST(ISNULL(time1 - time2, 0), GETDATE()) AS INT)
FROM table
WHERE field = 111