TRY_PARSE 什么时候能找到用特殊字符包裹的有效日期?

When will TRY_PARSE find a valid date wrapped in special characters?

我正在尝试了解 TRY_PARSE 的实际工作原理。我已通读 Documentation for TRY_PARSE by Microsoft。在我 运行 我自己的一些测试之前,文档是有意义的。使用 2022-01-26T12:00:00.000Z#2022-01-26T12:00:00.000Z# 我将从 TRY_PARSE 函数返回一个有效的 DateTime;但是,使用 !2022-01-26T12:00:00.000Z! 我将从 TRY_PARSE 函数返回 null。

可以用哪些特殊字符来包裹日期?为什么 # 有效但 ! 无效?

TRY_PARSE 函数使用 .NET CLR 来解析值,因此 TRY_PARSE(@s As datetime) 的规则与 .NET 的 DateTime.TryParse 方法相同:

DateTime.Parse Method (System) | Microsoft Docs
Any leading, inner, or trailing white space character in s is ignored. The date and time can be bracketed with a pair of leading and trailing NUMBER SIGN characters ("#", U+0023), and can be trailed with one or more NULL characters (U+0000).

如果您的字符串在日期前后使用除 # 以外的任何“特殊”字符,它将无法解析。

此外,如果您在约会的开始或结束时有 mis-matched #,它不会解析。