Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Getdate()等效于Jet/Access数据库。需要上个月的记录吗_Sql_Visual Studio_Visual Studio 2008_Ms Access - Fatal编程技术网

Sql Getdate()等效于Jet/Access数据库。需要上个月的记录吗

Sql Getdate()等效于Jet/Access数据库。需要上个月的记录吗,sql,visual-studio,visual-studio-2008,ms-access,Sql,Visual Studio,Visual Studio 2008,Ms Access,我在阅读其他发布的问题,发现了许多检索上个月记录的例子。我正在使用Visual Studio 2008 query builder从Access mdb检索记录,当我输入以下查询时,它向我显示一个错误,即getdate不是有效函数: where [Transaction Date] between dateadd(mm, datediff(mm, 0, dateadd(MM, -1, getdate())), 0) and dateadd(ms, -3, datea

我在阅读其他发布的问题,发现了许多检索上个月记录的例子。我正在使用Visual Studio 2008 query builder从Access mdb检索记录,当我输入以下查询时,它向我显示一个错误,即getdate不是有效函数:

where [Transaction Date]     
   between dateadd(mm, datediff(mm, 0, dateadd(MM, -1, getdate())), 0)
       and dateadd(ms, -3, dateadd(mm, datediff(mm, 0, dateadd(MM, -1, getdate())) + 1, 0))
从mdb中提取上月记录的正确sql查询是什么

这是我的一个查询,但它给了我本月的记录,amd上个月也需要:

  SELECT
   [Product Code], [Description One], [Transaction Number], Quantity, [Sales Value], Cost, [Transaction Date], [Transaction Time], Department, [Type Code], Cashier, [Computer Name], [Customer Code]
  FROM
   [Product History] 
  WHERE
   ([Transaction Date] >= DATEADD('m', - 2, NOW()))
 DateSerial(Year(Date()),Month(Date()),0)
 DateSerial(Year(Date()),Month(Date())-1,1)

非常感谢您的帮助。

access中的Getdate()等价物是Now()。

我倾向于在access中创建一个自定义函数来计算下个月的开始和结束以及其他常见日期。下面是定义了下个月开始和下个月结束的函数示例

Public Function Common_dates_SQL(strCommon_date As String) As Date
On Error GoTo Error_trap:

Select Case strCommon_date

    Case "Start_Last_Month"
        Common_dates_SQL = Date - ((DateDiff("d", DateValue("01/" & DatePart("m", Date) & "/" & DatePart("yyyy", Date)), Date)) + 1)

    Case "End_Last_Month"
    Common_dates_SQL = (Date - ((DateDiff("d", DateValue("01/" & DatePart("m", Date) & "/" & DatePart("yyyy", Date)), Date)) + 1)) - (DatePart("d", Date - ((DateDiff("d", DateValue("01/" & DatePart("m", Date) & "/" & DatePart("yyyy", Date)), Date)) + 1)) - 1)

End Select
DoCmd.Hourglass False
Exit Function

Error_trap:
DoCmd.Hourglass False
MsgBox "An error happened in sub Common_dates, error description " & Err.Description, vbCritical, "FRapps"

End Function
完整的功能会持续更长的时间,包括季度/年度和其他我需要的东西

然后,您可以像这样在SQL查询中使用此函数

SELECT tblFoo.*
FROM tblFoo
WHERE (((Created_date) Between Common_dates_SQL('Start_last_month') And Common_dates_SQL('END_last_month')));

一个月的第0天是上个月的最后一天,这在Jet SQL和VBA中都有效

上月底:

  SELECT
   [Product Code], [Description One], [Transaction Number], Quantity, [Sales Value], Cost, [Transaction Date], [Transaction Time], Department, [Type Code], Cashier, [Computer Name], [Customer Code]
  FROM
   [Product History] 
  WHERE
   ([Transaction Date] >= DATEADD('m', - 2, NOW()))
 DateSerial(Year(Date()),Month(Date()),0)
 DateSerial(Year(Date()),Month(Date())-1,1)
上月初:

  SELECT
   [Product Code], [Description One], [Transaction Number], Quantity, [Sales Value], Cost, [Transaction Date], [Transaction Time], Department, [Type Code], Cashier, [Computer Name], [Customer Code]
  FROM
   [Product History] 
  WHERE
   ([Transaction Date] >= DATEADD('m', - 2, NOW()))
 DateSerial(Year(Date()),Month(Date()),0)
 DateSerial(Year(Date()),Month(Date())-1,1)

现在返回date&time,date返回date only,我认为date更适合此查询。不过,我认为我的说法是正确的,如果您在Transaction date列上有一个索引,它将不会用于此查询,这可能会导致查询性能不理想。然而,我愿意纠正这一点,但这肯定是我的经验,这是我一直在寻找的。非常感谢你!!UDF确实会减慢查询速度,并且不应该是日期所必需的。我应该补充的是,我使用这些函数的大部分时间都是在动态生成的SQL查询中,因此传递的SQL只是该日期,而不是UDF。然而,我很少在SQL查询中直接使用它们,但我不能评论性能,因为我唯一一次使用它们是在小查询中