日期时间的小数位数过多
我有以下代码:
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
// set properties on builder (omitted for brevity)
using (SqlConnection connection = new SqlConnection(builder.ToString()))
{
connection.Open();
using (SqlCommand command = connection.CreateCommand())
{
command.CommandType = System.Data.CommandType.Text;
command.CommandText = "SELECT * FROM Employee WHERE WhenHire < @HireDate";
SqlParameter hireDateParameter = new SqlParameter("@HireDate", DateTime.Now);
command.Parameters.Add(hireDateParameter);
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
// do stuff
}
}
}
}
在 SQL Profiler 中显示为:
exec sp_executesql N'SELECT * FROM Employee WHERE WhenHire < @HireDate',
N'@HireDate datetime',
@HireDate='2010-12-06 11:43:23.573'
如何确定日期时间参数的精度?我见过小数点后有 7 位数字。
exec sp_executesql N'SELECT * FROM Employee WHERE WhenHire < @HireDate',
N'@HireDate datetime',
@HireDate='2010-12-06 11:43:23.5733125'
在这种情况下,该语句将无法执行并出现以下错误:
消息 8114,级别 16,状态 1,第 1 行 将数据类型 varchar 转换为时出错 日期时间。
I have the following code:
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
// set properties on builder (omitted for brevity)
using (SqlConnection connection = new SqlConnection(builder.ToString()))
{
connection.Open();
using (SqlCommand command = connection.CreateCommand())
{
command.CommandType = System.Data.CommandType.Text;
command.CommandText = "SELECT * FROM Employee WHERE WhenHire < @HireDate";
SqlParameter hireDateParameter = new SqlParameter("@HireDate", DateTime.Now);
command.Parameters.Add(hireDateParameter);
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
// do stuff
}
}
}
}
Which shows up in SQL Profiler as:
exec sp_executesql N'SELECT * FROM Employee WHERE WhenHire < @HireDate',
N'@HireDate datetime',
@HireDate='2010-12-06 11:43:23.573'
How is the precision of the datetime parameter determined? I've seen this have 7 digits after the decimal.
exec sp_executesql N'SELECT * FROM Employee WHERE WhenHire < @HireDate',
N'@HireDate datetime',
@HireDate='2010-12-06 11:43:23.5733125'
In that case, the statement fails to execute with this error:
Msg 8114, Level 16, State 1, Line 1
Error converting data type varchar to
datetime.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看起来这是 2008 Profiler 中的一个错误:
http://blogs.msdn.com/b/sqlserverfaq/archive/2009/08/ 13/sql-server-2008-profiler-pads-the-datetime-datatype-with-0s-when-tracing-for-a-ado-net-application.aspx
Looks like this is a bug in the 2008 Profiler:
http://blogs.msdn.com/b/sqlserverfaq/archive/2009/08/13/sql-server-2008-profiler-pads-the-datetime-datatype-with-0s-when-tracing-for-a-ado-net-application.aspx
根据 Microsoft (http://msdn.microsoft.com/en-us/library/ms187819.aspx),T-SQL DateTime 类型的精度为 0.000、0.003 或 0.007 毫秒。任何比这更精确的东西都可能导致该错误。
除非您确实需要精确到毫秒,否则我会给 DateTime.Now 一个格式字符串 - 类似于“yyyy-MM-dd HH:mm:ss” - 这将为您提供:
.NET Framework 能够报告以毫秒为间隔,精确到小数点后七位,因此,如果源数据来自 DateTime.Now,则可能会传入最多七位小数。
Per Microsoft (http://msdn.microsoft.com/en-us/library/ms187819.aspx), the T-SQL DateTime type has an accuracy of .000, .003, or .007 milliseconds. Anything with more precision than that could cause that error.
Unless you really need accuracy down to the millisecond, I would give DateTime.Now a format string -- something like "yyyy-MM-dd HH:mm:ss" -- which would give you:
The .NET Framework is able to report on millisecond intervals down to seven decimal points, so if your source data is coming from DateTime.Now, you could potentially get up to seven decimal points passed in.
我建议确保您使用带有
DateTime
数据库类型的SqlParameter
- 尝试以下代码:I would recommend making sure you use a
SqlParameter
with aDateTime
db type - try this code:http://msdn.microsoft.com/en-us/library/ms186724.aspx
微软还提供多种日期和时间数据类型。一种是 datetime2,它提高了精度和用户定义的小数秒精度。
他们给出了一个具有 7 位亚秒精度的示例 datetime2。
http://msdn.microsoft.com/en-us/library/ms186724.aspx
Also from microsoft there are multiple date and time data types. One is datetime2 which has increased accuracy and user-defined fractional second precision.
They give an example datetime2 with 7 digits of sub second precision.
看来客户端机器有些奇怪。不确定它到底是什么,但是当我们转移到另一台机器时,它不再增加额外的精度。感谢所有的想法。
It looks like it was something strange with the client machine. Not sure exactly what it was, but when we moved to another machine, it no longer added that extra precision. Thanks for all the ideas.