varchar转换为Datetime [英] Conversion of varchar into Datetime

查看:268
本文介绍了varchar转换为Datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码为什么起作用:

Why does the following code work:

CAST(DateOfAction AS Date)

,但此代码不会:

CAST(DateOfAction AS Datetime)

注意: DateOfAction varchar 字段

有人可以解释一下并给出正确的代码吗?

Can someone explain this and give the right code?

推荐答案

除指出该日期的其他答案外,某些转换并非日期时间也是安全的,还存在日期范围大于日期时间范围。因此,如果您的日期早于1753年,则格式/转换操作将无效。

Besides the other answers pointing out that date has certain conversions that are safe that aren't for datetime, there's also the fact that the range of date is larger than that for datetime. So if you have dates earlier than 1753, no amount of formatting/conversion will work.

此外,如果这不是 t用于将代码转换为在较旧的服务器上工作,但只是为了获取时间成分,那么如果您在服务器上有 date 个日期,则应该有 datetime2 ,效果会更好。

Also, if this isn't for converting code to work on an older server, but just to gain a time component, then if you've got date available on your server, you should have datetime2, which will work a lot better.

这篇关于varchar转换为Datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆