datetime2 是否有类似 isdate() 的函数? [英] Is there a function like isdate() for datetime2?
问题描述
我知道有一个名为 ISDATE
的函数来验证 DATETIME
列,但它仅适用于 SMALLDATETIME
和 DATETIME
类型.
I know there is a function called ISDATE
to validate DATETIME
columns, but it works only for the SMALLDATETIME
and DATETIME
types.
是否有类似的方法来验证 SQL Server 2008 和 2012 中的新数据类型 DATETIME2
?
Is there a similar way to validate the new data type DATETIME2
in SQL Server 2008 and 2012?
推荐答案
在 SQL Server 2012 中,您可以使用 TRY_CONVERT
:
In SQL Server 2012, you can use TRY_CONVERT
:
SELECT TRY_CONVERT(DATETIME2, '2012-02-02 13:42:55.2323623'),
TRY_CONVERT(DATETIME2, '2012-02-31 13:42:55.2323623');
结果:
2012-02-02 13:42:55.2323623 NULL
或TRY_PARSE
:
SELECT TRY_PARSE('2012-02-02 13:42:55.2323623' AS DATETIME2),
TRY_PARSE('2012-02-31 13:42:55.2323623' AS DATETIME2);
(相同的结果.)
对不起,我没有为您提供一个聪明的答案<SQL Server 2012.我猜你可以说
Sorry that I don't have a clever answer for you for < SQL Server 2012. You could, I guess, say
SELECT ISDATE(LEFT('2012-02-02 13:42:55.2323623', 23));
但这感觉很脏.
TRY_CONVERT代码> Microsoft Docs 上的文档
TRY_PARSE
文档在 Microsoft Docs 上
TRY_CONVERT
documentation on Microsoft Docs
TRY_PARSE
documentation on Microsoft Docs
这篇关于datetime2 是否有类似 isdate() 的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!