SQL 省略日期中的毫秒数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2246655/
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
Omitting the Milliseconds in a Date
提问by Brian Mains
When I select from SQL Server, I want to get a date, but omit the millisecond value, and I want it to be as a date type. So if I have a value 1/1/2009 1:23:11.923
, I want to omit the millisecond but retain the date type, so that it will be the value 1/1/2009 1:23:11.000
(I know you really can't omit the millisecond value with a date, just want it to be zero).
当我从 SQL Server 中选择时,我想获取一个日期,但省略了毫秒值,我希望它作为日期类型。因此,如果我有一个 value 1/1/2009 1:23:11.923
,我想省略毫秒但保留日期类型,以便它成为值1/1/2009 1:23:11.000
(我知道你真的不能省略带有日期的毫秒值,只是希望它为零)。
Is there a function in SQL Server to do this? Or do I have to write my own function? Again, I don't want it as a varchar
type, but a datetime
type.
SQL Server 中是否有函数可以执行此操作?还是我必须编写自己的函数?同样,我不希望它是一种varchar
类型,而是一种datetime
类型。
采纳答案by Amy B
If you don't want to use string conversions, here's a solution:
如果您不想使用字符串转换,这里有一个解决方案:
DECLARE @TheDate datetime, @Today datetime
SET @TheDate = GetDate()
SET @Today = DateAdd(dd, DateDiff(dd, 0, @TheDate), 0)
SELECT DateAdd(s, DateDiff(s, @Today, @TheDate), @Today)
回答by Peter Radocchia
Use DATETIME2
, a new datatype in SQL Server 2008 that supports fractional precision:
使用DATETIME2
,SQL Server 2008 中支持小数精度的新数据类型:
SELECT
CONVERT(DATETIME2(0),SYSDATETIME()) [yyyy-mm-dd hh:mm:ss]
, CONVERT(DATETIME2(1),SYSDATETIME()) [yyyy-mm-dd hh:mm:ss.f]
, CONVERT(DATETIME2(2),SYSDATETIME()) [yyyy-mm-dd hh:mm:ss.ff]
, CONVERT(DATETIME2(3),SYSDATETIME()) [yyyy-mm-dd hh:mm:ss.fff]
, CONVERT(DATETIME2(4),SYSDATETIME()) [yyyy-mm-dd hh:mm:ss.ffff]
, CONVERT(DATETIME2(5),SYSDATETIME()) [yyyy-mm-dd hh:mm:ss.fffff]
, CONVERT(DATETIME2(6),SYSDATETIME()) [yyyy-mm-dd hh:mm:ss.ffffff]
, CONVERT(DATETIME2(7),SYSDATETIME()) [yyyy-mm-dd hh:mm:ss.fffffff]
The conversion will round to the nearest unit, eg:
转换将四舍五入到最接近的单位,例如:
2014-09-04 09:35:47.0162993 as DATETIME2(4) ->
2014-09-04 09:35:47.0163
Alternatively, on SQL 2005 and eariler:
或者,在 SQL 2005 及更早版本上:
SELECT
original = GETDATE()
, [floor] = DATEADD(ms,-DATEPART(ms,GETDATE()),GETDATE())
, [ceiling] = DATEADD(ms,1000-DATEPART(ms,GETDATE()),GETDATE())
, [rounded] = DATEADD(ms,CASE WHEN DATEPART(ms,GETDATE()) < 500 THEN 0 ELSE 1000 END-DATEPART(ms,GETDATE()),GETDATE())
This is a bit faster than converting to and from a string representation.
这比在字符串表示之间进行转换要快一些。
回答by OMG Ponies
Use:
用:
SELECT CONVERT(DATETIME, CONVERT(VARCHAR(19), GETDATE(), 120))
This:
这个:
CONVERT(VARCHAR(19), GETDATE(), 120)
...omits the milliseconds, returning a VARCHAR. So you CAST/CONVERT that into a DATETIME in order to work with the desired data type.
...省略毫秒,返回一个 VARCHAR。因此,您将其 CAST/CONVERT 转换为 DATETIME 以使用所需的数据类型。
See this link for a list of various date/time formats you can work with.
有关您可以使用的各种日期/时间格式的列表,请参阅此链接。
回答by SQLMenace
try this
尝试这个
declare @DATE datetime
select @DATE = '1/1/2009 1:23:11.923'
SELECT convert(datetime,CONVERT(char(35),@DATE,120))
or with date functions only
或仅使用日期函数
DECLARE @DATE DATETIME
SELECT @DATE = '1/1/2009 1:23:11.923'
SELECT DATEADD(SECOND, DATEDIFF(SECOND, 39000, @DATE), 39000)
回答by Quassnoi
SELECT GETDATE(),
CONVERT(DATETIME, CONVERT(VARCHAR(MAX), GETDATE(), 120), 120)
回答by LCJ
Subtract millisecond from date. (Or add negative value of millisecond)
从日期中减去毫秒。(或添加负值毫秒)
SELECT DATEADD(ms, -DATEPART(ms, GETDATE()), GETDATE())
回答by Frank Kalis
DATEADD(SECOND, DATEDIFF(SECOND, 0, < your datetime column >), 0)
May need to change the 0 to something else to prevent an overflow error. Don't have a SQL Server at hand right now to verify.
可能需要将 0 更改为其他值以防止溢出错误。现在手头没有 SQL Server 进行验证。
While this method does not appear to be intuitive at first sight, have a look here for the rationale behind it: http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes
虽然这种方法乍一看似乎并不直观,但请在此处查看其背后的原理:http: //karaszi.com/the-ultimate-guide-to-the-datetime-datatypes