获取月中的第一天(和最后一天) - 表达式简化

时间:2014-03-10 17:03:07

标签: sql date firebird

我需要在当前(和之前)月的第一天之前获得日期,即30天(之后我将使用90或18O)。我写了这个,但我认为这是不必要的复杂,所以我来这里寻求帮助。这有什么办法可以简化这个:

ib_encodedate(EXTRACT(YEAR from (dateadd(-30-datediff(day from cast(EXTRACT(MONTH from CURRENT_DATE) 
|| '-1-' 
|| EXTRACT(YEAR from CURRENT_DATE) as date) to date 'now') DAY to CURRENT_DATE))), EXTRACT(MONTH from (dateadd(-30-datediff(day from cast(EXTRACT(MONTH from CURRENT_DATE) 
|| '-1-' 
|| EXTRACT(YEAR from CURRENT_DATE) as date) to date 'now') DAY to CURRENT_DATE))),EXTRACT(DAY from (dateadd(-30-datediff(day from cast(EXTRACT(MONTH from CURRENT_DATE) 
|| '-1-' 
|| EXTRACT(YEAR from CURRENT_DATE) as date) to date 'now') DAY to CURRENT_DATE))))

在数据库中,我的日期为double,因此我必须使用函数ib_encodedate将日期转换为double并与数据库中的日期进行比较。功能有原型:

ib_encodedate(INT year, INT month, INT day)

我需要写一个月的最后一天。

感谢您的帮助。

7 个答案:

答案 0 :(得分:7)

好像你需要这样的东西:

SELECT
    DATEADD (-EXTRACT(DAY FROM CURRENT_DATE)+1 DAY TO CURRENT_DATE) AS FIRST_DAY_OF_MONTH,
    DATEADD (-30 DAY TO DATEADD (-EXTRACT(DAY FROM CURRENT_DATE)+1 DAY TO CURRENT_DATE)) AS A_MONTH_AGO,
    DATEADD (-90 DAY TO DATEADD (-EXTRACT(DAY FROM CURRENT_DATE)+1 DAY TO CURRENT_DATE)) AS THREE_MONTHS_AGO,
    DATEADD (-180 DAY TO DATEADD (-EXTRACT(DAY FROM CURRENT_DATE)+1 DAY TO CURRENT_DATE)) AS SIX_MONTHS_AGO
FROM 
    RDB$DATABASE

使用firebird中的DATEADD函数,您可以轻松完成此操作。

答案 1 :(得分:5)

日期的第一天:

select cast(:DATA as date) - extract(day from cast(:DATA as date)) + 1 from RDB$DATABASE

日期的最后一天:

select cast(:DATA as date) - extract(day from cast(:DATA as date)) + 32 - extract(day from (cast(:DATA as date) - extract(day from cast(:DATA as date)) + 32)) from RDB$DATABASE

答案 2 :(得分:3)

使用Firebird 2.5

第一天:

SELECT CAST(EXTRACT(YEAR FROM CURRENT_DATE)||'-'||EXTRACT(MONTH FROM 
       CURRENT_DATE)||'-01' AS DATE) FROM RDB$DATABASE

最后一天:

SELECT DATEADD (-1 DAY TO DATEADD (1 MONTH TO CAST(EXTRACT(YEAR FROM 
                CURRENT_DATE)||'-'||EXTRACT(MONTH FROM CURRENT_DATE)||'-01' AS DATE))) 
      FROM RDB$DATABASE

答案 3 :(得分:0)

如果使用firebird 1.5,则可以创建此函数,该函数返回指定日期的月份的最后一天:

create or alter procedure LAST_DAY_OF_MONTH (
    REFERENCE_DATE date)
returns (
    LAST_DAY date)
as
declare variable NEXT_DAY date;
declare variable COUNTER integer;
begin
    NEXT_DAY = :REFERENCE_DATE;
    COUNTER = 0;
    while (:COUNTER < 33) do begin
        NEXT_DAY = :NEXT_DAY + 1;
        if (extract(month from :NEXT_DAY) = extract(month from :REFERENCE_DATE)) then begin
            LAST_DAY = :NEXT_DAY;
        end
        COUNTER = :COUNTER + 1;
    end
    suspend;
end

然后调用它只执行:

select last_day from last_day_of_month('02-23-2016')

这将返回02-29-2016

答案 4 :(得分:0)

select dateadd (month, -1,dateadd (-extract(day from current_date)+1 day to current_date)) First_day_month_ago 
from RDB$DATABASE 
union all
select dateadd (-extract(day from current_date)+1 day to current_date) First_day_current_month 
from RDB$DATABASE

答案 5 :(得分:0)

从MySQL迁移,我需要FIRST_DAY和LAST_DAY函数。他们在这里:

CREATE OR ALTER FUNCTION FIRST_DAY(d TIMESTAMP)
RETURNS DATE
AS 
BEGIN
   RETURN CAST(d - EXTRACT(DAY FROM d) + 1 AS DATE); 
END

CREATE OR ALTER FUNCTION LAST_DAY(d TIMESTAMP)
RETURNS DATE
AS 
BEGIN
   RETURN DATEADD(1 MONTH TO FIRST_DAY(d)) - 1;  
END

并从isql执行:

SQL> SELECT first_day(CURRENT_TIMESTAMP), last_day(CURRENT_TIMESTAMP) FROM RDB$DATABASE rd;

  FIRST_DAY    LAST_DAY 
=========== =========== 
2020-08-01  2020-08-31  

答案 6 :(得分:-1)

CREATE PROCEDURE FIRST_LAST_DAYOFMONTH(COMPARE_DATE TIMESTAMP)
RETURNS (FIRST_DAYOFOMONTH TIMESTAMP,LAST_DAYOFMONTH TIMESTAM)
AS
BEGIN
FIRST_DAYOFMONTH=COMPARE_DATE-(EXTRACT(DAY FROM :COMPARE_DATE)-1);
LAST_DAYOFMONTH=DATEADD(1 MONT TO :COMPARE_DATE)-1;
SUSPEND;
END^