使用“之间” 与 varchar (sql server)

发布于 2024-07-22 06:00:24 字数 675 浏览 3 评论 0原文

使用 SQL Server 2005 Express。

(
    CONVERT(VARCHAR(8), R.reviewStart, 108) between CONVERT(VARCHAR(8), M.meetingStart, 108) and CONVERT(VARCHAR(8), M.meetingEnd, 108) OR
    CONVERT(VARCHAR(8), R.reviewEnd, 108) between CONVERT(VARCHAR(8), M.meetingStart, 108) and CONVERT(VARCHAR(8), M.meetingEnd, 108) OR
    CONVERT(VARCHAR(8), M.meetingStart, 108) between CONVERT(VARCHAR(8), R.reviewStart, 108) and CONVERT(VARCHAR(8), R.reviewEnd, 108) OR
    CONVERT(VARCHAR(8), M.meetingEnd, 108) between CONVERT(VARCHAR(8), R.reviewStart, 108) and CONVERT(VARCHAR(8), R.reviewEnd, 108)
)

日期时间转换为 varchar 后,“ Between ”是否仍具有预期的行为?

谢谢

Using SQL Server 2005 Express.

(
    CONVERT(VARCHAR(8), R.reviewStart, 108) between CONVERT(VARCHAR(8), M.meetingStart, 108) and CONVERT(VARCHAR(8), M.meetingEnd, 108) OR
    CONVERT(VARCHAR(8), R.reviewEnd, 108) between CONVERT(VARCHAR(8), M.meetingStart, 108) and CONVERT(VARCHAR(8), M.meetingEnd, 108) OR
    CONVERT(VARCHAR(8), M.meetingStart, 108) between CONVERT(VARCHAR(8), R.reviewStart, 108) and CONVERT(VARCHAR(8), R.reviewEnd, 108) OR
    CONVERT(VARCHAR(8), M.meetingEnd, 108) between CONVERT(VARCHAR(8), R.reviewStart, 108) and CONVERT(VARCHAR(8), R.reviewEnd, 108)
)

Will the "between" still have the expected behavior after the datetimes have been converted to varchar?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋凉 2024-07-29 06:00:24

是的,取决于您所说的预期行为是什么意思。 BETWEEN 运算符会将这些操作数视为 varchar,并相应地应用其比较规则:

如果值介于 BETWEEN 之间,则返回 TRUE
test_expression 的值大于或
等于 begin_expression 的值
并且小于或等于的值
结束表达式。

现在,我可以看到很多潜在的问题,比较字符串并期望日期比较行为。 我在测试中没有看到任何内容,但请仔细查看您的数据。 CONVERT 是否返回 24 小时时间并带有适当的前导零?

这个问题还有一些其他方法来比较无日期时间,除了将它们转换为 varchar 之外。

另外,请注意空日期,这将导致相应的 WHERE 条件返回 false(实际上,未知)。

在您的另一个问题中,您表示您收到了错误。 如果是这样,您可以发布吗?

Yes, depending on what you mean by expected behavior. The BETWEEN operator will treat these operands as varchars, and apply its comparison rules accordingly:

BETWEEN returns TRUE if the value
of test_expression is greater than or
equal to the value of begin_expression
and less than or equal to the value of
end_expression.

Now, I can see a lot of potential problems, comparing strings and expecting date comparison behavior. I haven't seen any in my tests, but look carefully at your data. Is the CONVERT returning 24-hour time, with the appropriate leading zeroes?

This question has some other approaches to comparing dateless-times, other than converting them to varchars.

Also, watch for null dates, which will cause the corresponding WHERE condition to return false (actually, unknown).

In your other question, you indicated that you were getting an error. If so, can you post that?

苏辞 2024-07-29 06:00:24

正如我在您的另一篇文章中所说,如果可能的话,您应该考虑迁移到 SQL 2008,因为 t

As I said in your other post if is possible you should consider moving to SQL 2008 because of the new datetime types that allow explicit separation of date part and time part so your filter expressions become much simpler and you can index by time. Since its Express there really shouldn't be any reason to hold you back at 2005.

梦太阳 2024-07-29 06:00:24

您的第一个条件相当于这个对索引更友好的条件:(

R.reviewStart >=  DATEADD(day, DATEDIFF(day, '19010101', M.meetingStart), '19010101') 
and R.reviewStart < DATEADD(day, 1+DATEDIFF(day, '19010101', M.meetingStart), '19010101') 

此处解释:
重用您的代码表值 UDF

Your first condition is equivalent to this more index friendly one:

R.reviewStart >=  DATEADD(day, DATEDIFF(day, '19010101', M.meetingStart), '19010101') 
and R.reviewStart < DATEADD(day, 1+DATEDIFF(day, '19010101', M.meetingStart), '19010101') 

(explained here:
Reuse Your Code with Table-Valued UDFs
)
.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文