比较没有当前月份的 SQL 日期

发布于 2024-09-28 07:43:57 字数 272 浏览 1 评论 0原文

我在 Access 中有一个表,其中包含字符串列和日期列。我想获取表中日期低于 2010 年 10 月 22 日(本月除外)的所有行。所以,我需要从 30.09.2010 到...的行

我绑定了一些东西,但我发现这是不对的:

SELECT name FROM table WHERE YEAR(date)<=2010 AND MONTH(date)<10

但是这个解决方案不好,我没有其他想法。您能帮我提供一个通用的解决方案吗? 谢谢

I have a table in Access with string columns and a date column. I want to get all the rows from the table when the date is lower than 22.10.2010, except this month. So, i need the rows from 30.09.2010 to ...

I tied something, but I figured out it's not right:

SELECT name FROM table WHERE YEAR(date)<=2010 AND MONTH(date)<10

But this solution it's not good, and i have no other idea. Could you help me with a general solution, pls?
Thanks

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

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

发布评论

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

评论(6

吝吻 2024-10-05 07:43:57

一个月的第 0 天是上个月的最后一天:

DateSerial(Year(Date()),Month(Date()),0)

因此:

 SELECT [name] FROM [table] 
 WHERE [date]<=DateSerial(Year(Date()),Month(Date()),0)

The zeroth day of a month is the last day of the previous month:

DateSerial(Year(Date()),Month(Date()),0)

Therefore:

 SELECT [name] FROM [table] 
 WHERE [date]<=DateSerial(Year(Date()),Month(Date()),0)
萝莉病 2024-10-05 07:43:57

SELECT name FROM table WHERE ( YEAR(date)<2010 ) OR ( YEAR(date)=2010 AND MONTH(date)<10 )

SELECT name FROM table WHERE ( YEAR(date)<2010 ) OR ( YEAR(date)=2010 AND MONTH(date)<10 )

心作怪 2024-10-05 07:43:57

使用权?

将年、月和“1”串在一起(以获取 date 月份的第一天)并将其转换为 Date

SELECT  *
FROM    MyTable
WHERE   dateColumn 
           < CDate(CStr(YEAR(date)) + "-" + CStr(MONTH(date)) + "-1")

SQL

从日期中减去DAY(减一)即可得到该月的第一天。然后返回小于该值的所有行。

DECLARE @date DATE
SET @date = GETDATE()DECLARE

SELECT  *
FROM    MyTable
WHERE   dateColumn 
           < DATEADD(DAY, -( DATEPART(DAY, @date) - 1 ), @date)

Access?

String together the year, month, and "1" (to get the first day of the month of date) and convert that to a Date.

SELECT  *
FROM    MyTable
WHERE   dateColumn 
           < CDate(CStr(YEAR(date)) + "-" + CStr(MONTH(date)) + "-1")

SQL

Subtract the DAY (minus one) from the date in question from the date to get the first of the month. Then return all rows less than this value.

DECLARE @date DATE
SET @date = GETDATE()DECLARE

SELECT  *
FROM    MyTable
WHERE   dateColumn 
           < DATEADD(DAY, -( DATEPART(DAY, @date) - 1 ), @date)
洒一地阳光 2024-10-05 07:43:57

您减去到目前为止该月的天数并使用该日期进行比较

Select myName FROM myTable Where myTable.myDate <=DateAdd("d",-Day(Now()),Now());

上面的查询将为您提供直到上个月最后一天结束时的所有记录。

干杯。

you subtract the number of days in the month so far and use that date for the comparison

Select myName FROM myTable Where myTable.myDate <=DateAdd("d",-Day(Now()),Now());

The above query will give you all the records till the end of the last day of the last month.

Cheers.

仅此而已 2024-10-05 07:43:57
SELECT you_col
  FROM YourTable
 WHERE your_date 
          < DATEADD('M', 
              DATEDIFF('M', #1990-01-01 00:00:00#, NOW()
          ), #1990-01-01 00:00:00#);
SELECT you_col
  FROM YourTable
 WHERE your_date 
          < DATEADD('M', 
              DATEDIFF('M', #1990-01-01 00:00:00#, NOW()
          ), #1990-01-01 00:00:00#);
恰似旧人归 2024-10-05 07:43:57

可以使用以下功能。这些甚至适用于闰年。

'如果你通过#2016/01/20#,你会得到#2016/01/31#

Public Function GetLastDate(tempDate As Date) As Date
    GetLastDate = DateSerial(Year(tempDate), Month(tempDate) + 1, 0)
End Function

'如果你通过#2016/01/20#,你会得到31

Public Function GetLastDay(tempDate As Date) As Integer
    GetLastDay = Day(DateSerial(Year(tempDate), Month(tempDate) + 1, 0))
End Function

The below functions can be used. These even work for leap years.

'If you pass #2016/01/20# you get #2016/01/31#

Public Function GetLastDate(tempDate As Date) As Date
    GetLastDate = DateSerial(Year(tempDate), Month(tempDate) + 1, 0)
End Function

'If you pass #2016/01/20# you get 31

Public Function GetLastDay(tempDate As Date) As Integer
    GetLastDay = Day(DateSerial(Year(tempDate), Month(tempDate) + 1, 0))
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文