2

我正在尝试从数据库表(adempiere)字段“dateinvoice”中提取前 1 或 2 个月的所有日期

例如,如果目前是 2012 年 1 月 6 日

我想提取 2011 年 12 月 1 日至 2011 年 12 月 31 日(前 1 个月)或 2011 年 11 月 1 日至 2011 年 12 月 31 日(前 2 个月)的所有数据

我已经看到一些使用 DateAdd 的答案,我最近了解到,由于我使用的是 PostgreSQL,DateAdd 函数不起作用。

我正在使用 PostgreSQL 9.1。

更新 我已经遵循 Craigs 的建议并使用 SQLFiddle,这是我第一次使用它,所以如果我有任何嘘声,请见谅

链接在这里 http://sqlfiddle.com/#!1/1d5d4/1 http://sqlfiddle.com/#!1/1d5d4/3

即使将月份更改为几个月后,我仍然会遇到错误......

更新编号 2 我使用 Visual Studio 搜索所有这些查询。克雷格的建议确实有效。我在查询设计器中时遇到了错误,但是当我编辑为文本时。似乎没有问题。

我不确定是否由于查询设计器而出现此错误。但是 Craig 的查询确实有效.. 我认为我只是坚持文本而不是 GUI Visual Studio 为这个问题提供的。谢谢!

4

3 回答 3

4

You likely want something like:

SELECT *
FROM the_table
WHERE date_column BETWEEN '2013-01-01' AND '2013-01-01' + INTERVAL '1' MONTH;

The BETWEEN operator is left-and-right inclusive. If you want right-exclusive you have to use separate >= and < tests instead.

Except for the date_trunc function that's ANSI-standard SQL, by the way, it's just that Microsoft SQL Server doesn't implement the ANSI interval types or ANSI date maths. Doing it a strictly ANSI-standard way would require you to replace the PostgreSQL-specific date_trunc with interval maths, like this example of how to get the months I just cooked up in Oracle. It's based on a subset of the sample data because of limitations in SQLFiddle for Oracle.

If you want to get the start of the current month, use date_trunc, eg:

SELECT date_trunc('2013-01-12');

will return 2013-01-01. This can be combined with INTERVAL computations and the extract operation to do pretty much anything you need to with dates and times.

In this case, for the month before last I'd write:

SELECT *
FROM the_table
WHERE date_field BETWEEN date_trunc('month',current_date) - INTERVAL '2' MONTH 
                     AND (date_trunc('month',current_date) - INTERVAL '1' MONTH) - INTERVAL  '1' DAY;

(You want INTERVAL '1' SECOND instead of INTERVAL '1' DAY if you're working with timestamps not dates).

You'll notice the explicit parens for the interval subtraction. That's quite important, because dates are horrible nasty things. In date computations, (a + b) + c isn't necessarily the same as a + (b + c). I was bitten by this recently, it's nasty.

Alternate phrasing, probably cleaner if you want a <= x < b:

SELECT *
FROM the_table
WHERE date_field >= date_trunc('month',current_date) - INTERVAL '2' MONTH
  AND date_field < date_trunc('month',current_date) - INTERVAL '1' MONTH;
于 2013-07-04T04:12:59.437 回答
1

尝试这个:

SELECT * from adempiere where dateinvoice >  CURRENT_DATE - INTERVAL '2 months'
于 2013-07-04T03:22:32.357 回答
0
CREATE FUNCTION [dbo].[GetDates](@StartDate DATETIME, @EndDate DATETIME)
RETURNS TABLE AS
RETURN (
WITH 
 N0 AS (SELECT 1 AS N UNION ALL SELECT 1)
,N1 AS (SELECT 1 AS N FROM N0 T1, N0 T2)
,N2 AS (SELECT 1 AS N FROM N1 T1, N1 T2)
,N3 AS (SELECT 1 AS N FROM N2 T1, N2 T2)
,N4 AS (SELECT 1 AS N FROM N3 T1, N3 T2)
,N5 AS (SELECT 1 AS N FROM N4 T1, N4 T2)
,N6 AS (SELECT 1 AS N FROM N5 T1, N5 T2)
,NUMS AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS NUM FROM N6)
SELECT CAST(DATEADD(day,num-1,@StartDate) AS Date) as TheDate
FROM NUMS 
WHERE NUM <= DATEDIFF(day,@StartDate,@EndDate) + 1
);

执行 SELECT * FROM dbo.GetDates('1 Dec 2011','31 Dec 2011');

于 2013-07-04T04:18:00.137 回答