如何将具体时间的当前日期分配给列? [英] How to assign current date with specific time to column?
问题描述
如何在特定时间内分配当前日期?
How do i assign current date with a specific time?
让我们说8:00:00 AM到数据类型的列EXIT_DT datetime
??
let's say 8:00:00 AM to Column EXIT_DT of datatype datetime
??
我尝试过 GETDATE()
AS EXIT_DT,但它给了我当前的日期时间。我正在使用Sql server 2005.任何帮助?让我们说今天是1/3/2013,我希望我的结果作为datetime数据类型返回,值为1/3/2013 8:00:00 AM。
I have tried GETDATE()
AS EXIT_DT but it gives me current datetime. I am using Sql server 2005. Any help?
如果我运行声明ytd,结果将是1/2/2013 8:00:00 AM
Lets say Today is 1/3/2013 and i want my result to return as a datetime datatype with value 1/3/2013 8:00:00 AM. If i run the statement ytd, the result will be 1/2/2013 8:00:00 AM
推荐答案
此公式将总是产生08:00的日期,并且避免字符串操作:
This formula will always produce 08:00 for the day it is called, and avoids string manipulation:
select DATEADD(day,DATEDIFF(day,'20010101',GETDATE()),'2001-01-01T08:00:00')
尝试避免转换为和从字符串转换的解决方案 - 将日期时间值视为字符串是最大的错误来源之一。
Try to avoid solutions that convert to and from strings - treating datetime values as strings is one of the largest sources of bugs.
它可以通过计算天数(如一个整数)从2001年1月1日起已经过去了,然后在2001年1月1日的08:00添加相同的天数。
It works by computing the number of days (as an integer) that have elapsed since 1st January 2001. It then adds that same number of days to 08:00 on 1st January 2001.
这篇关于如何将具体时间的当前日期分配给列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!