从字符串转换日期/或时间时,T-SQL 转换失败
Perfmon 在直接登录 SQL 时创建数据库的方式不太友好:
select top 1 Convert(datetime, CounterDateTime) from CounterData
返回
从字符串转换日期和/或时间时转换失败。
该单元格的值为“2012-01-25” 14:12:10.802"。在选择过程中将其转换为日期时间字段的正确方法是什么?
Perfmon isn't so kind with the way it creates the database when logging directly to SQL:
select top 1 Convert(datetime, CounterDateTime) from CounterData
returns
Conversion failed when converting date and/or time from character string.
The value of that cell is "2012-01-25 14:12:10.802". What is the proper way to convert this to a datetime field during selection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现以下方法有效(而不是 CHAR(24) ):
select top 1 Cast(Cast(CounterDateTime as CHAR(23)) as datetime) from CounterData
希望有更好的解决方案。
I figured out that the following works (instead of the
CHAR(24)
it is):select top 1 Cast(Cast(CounterDateTime as CHAR(23)) as datetime) from CounterData
Hoping there is a better solution though.
没有更好的答案,但这会让味道少一点苦味。
CAST(CAST(CounterData.CounterDateTime AS CHAR(NN)) AS DATETIME) AS CounterDateTime
还截断值,以便不需要额外的 T-SQL DateTime 截断为分钟、小时等。
No better answer but this makes the taste a trifle less bitter.
CAST(CAST(CounterData.CounterDateTime AS CHAR(NN)) AS DATETIME) AS CounterDateTime
Also truncates the value so that additional T-SQL DateTime truncation to the minute, hour, etc is not needed.