如何排除 BETWEEN sql server 中的上限

发布于 2024-12-23 03:05:35 字数 829 浏览 1 评论 0原文

我使用 SQL Server 作为我的数据库。我正在搜索我输入的日期的行。这意味着搜索 submission_date 恰好是 '12/13/2011' 的行。首先,我将搜索条件日期转换为毫秒

,即

Dec 13 2011 00:00:00 ='1323727200000'
Dec 14 2011 00:00:00 ='1323813600000'`

SELECT * 
FROM log_file 
WHERE submission_date BETWEEN '1323727200000' AND '1323813600000' 

此查询将搜索 Dec 13 MidnightDec 14 Midnight,但我想跳过上限值,即从 <代码>2011年12月13日00:00:00到2011年12月13日59:59:59。为此,我想使用 >= 和 <。这是正确的做法吗?

完成此操作后,我有一个 iBatis xml,其中我正在编写以下内容,这给了我错误。

<isNotEmpty prepend="AND" property="submissiondate">
   submission_date <![CDATA[ >= ]]> #submissiondate # AND <![CDATA[ < ]]> #submissiondate #
</isNotEmpty>

请建议这是否是正确的方法。

谢谢

I am using SQL Server as my database. I am searching for a row for the date that I have entered. This means searching rows where submission_date is exactly '12/13/2011'. First I am converting the search criteria date to milliseconds

i.e.

Dec 13 2011 00:00:00 ='1323727200000'
Dec 14 2011 00:00:00 ='1323813600000'`

SELECT * 
FROM log_file 
WHERE submission_date BETWEEN '1323727200000' AND '1323813600000' 

This query will search for Dec 13 Midnight to Dec 14 Midnight, but I want to skip the upper limit value i.e. from Dec 13 2011 00:00:00 to Dec 13 2011 59:59:59. For this I thought to use >= and <. Is this a right approach?

Having done this, I have a iBatis xml where I am writing the following which is giving me error.

<isNotEmpty prepend="AND" property="submissiondate">
   submission_date <![CDATA[ >= ]]> #submissiondate # AND <![CDATA[ < ]]> #submissiondate #
</isNotEmpty>

Please suggest if this is the right approach.

Thanks

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

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

发布评论

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

评论(3

昵称有卵用 2024-12-30 03:05:35

是的,您通常会使用 >=< 进行时间/日期范围查询。

或者,您可以从上限中减去 3 毫秒以获得最高日期时间 (不是较新的 datetime2) 当天的值 (xxx 23:59.59.997)

SELECT * FROM   log_file
WHERE  submission_date BETWEEN 1323714600000 AND 1323801000000-3

注意:如果一切都是毫秒,则减去 1 可能就可以了...

编辑,为什么 3ms 的示例

SELECT 
    DATEADD(millisecond, -1, '20111214'), -- 2011-12-14 00:00:00.000
    DATEADD(millisecond, -2, '20111214'), -- 2011-12-13 23:59:59.997
    DATEADD(millisecond, -3, '20111214')  -- 2011-12-13 23:59:59.997

以及有趣的是,你确定现在是午夜吗?
在 1323813600 秒内,我得到 2011-12-13 22:00:00

On SQL Server:

SELECT DATEADD(second, 1323813600, '19700101') 

On MySQL

SELECT FROM_UNIXTIME(1323813600)

Yes, you'd use >= and < typically for time/date range queries

Alternatively, you could subtract 3 milliseconds from the upper limit to get the highest datetime (not newer datetime2) value for that day (xxx 23:59.59.997)

SELECT * FROM   log_file
WHERE  submission_date BETWEEN 1323714600000 AND 1323801000000-3

Note: subtracting 1 would probably be OK if everything is milliseconds...

Edit, example of why 3ms

SELECT 
    DATEADD(millisecond, -1, '20111214'), -- 2011-12-14 00:00:00.000
    DATEADD(millisecond, -2, '20111214'), -- 2011-12-13 23:59:59.997
    DATEADD(millisecond, -3, '20111214')  -- 2011-12-13 23:59:59.997

And interestingly, are you sure this is midnight?
For 1323813600 seconds, I get 2011-12-13 22:00:00

On SQL Server:

SELECT DATEADD(second, 1323813600, '19700101') 

On MySQL

SELECT FROM_UNIXTIME(1323813600)
甜心 2024-12-30 03:05:35

在您的情况下,“日期”似乎是 BIGINT 类型,为什么不直接从间隔上限中减去 1 呢?

SELECT * FROM log_file 
WHERE submission_date BETWEEN 1323714600000 AND 1323801000000 - 1

当然,这不适用于浮点数或小数......

In your case, where "date" seems to be of type BIGINT, why not just subtract 1 from the upper interval limit?

SELECT * FROM log_file 
WHERE submission_date BETWEEN 1323714600000 AND 1323801000000 - 1

Of course, this wouldn't work with floating point numbers or decimals...

习惯成性 2024-12-30 03:05:35

是的,如果您必须跳过上限 - 您应该使用

WHERE Date >= '20111213' AND Date < '20111214'

当然 - 如果您的列的类型是 DATETIME

Yes, if you have to skip the upper limit - you should use

WHERE Date >= '20111213' AND Date < '20111214'

Of course - if your column's type is DATETIME

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