isdate 和新西兰返回 false

2021-09-10 00:00:00 tsql sql-server

问题来了:

select ISDATE('2012-06-21 4:11 P.M.') 

文化是 en-NZ(新西兰),上面的字符串日期是他们格式化日期的方式.

The culture is en-NZ (New Zealand) and the string date above is how they format their dates.

在这种情况下,我如何使 ISDATE 为真?

How do I make ISDATE true in this case?

推荐答案

理想情况下,您可以更好地控制字符串格式.如果您以标准格式传递,则不应关心文化是 EN-NZ 还是其他文化,例如:

Ideally you would control the string format better than that. You shouldn't care whether the culture is EN-NZ or anything else if you pass in a standard format, such as:

SELECT ISDATE('20120621 16:11'); -- will never fail regardless of locale/language/DMY

如果您需要允许人们以他们希望的任何格式输入日期,那么您必须以各种方式处理它.例如:

If you need to allow people to enter dates in any format they wish, then you have to deal with it in various ways. For example:

SELECT ISDATE(CONVERT(DATETIME, REPLACE('2012-06-21 4:11 P.M.', '.', ''), 120));

相关文章