2016-09-16 109 views
-1

有人可以將這個函數從SQL Server轉換爲PostgreSQL嗎?將函數從SQL Server轉換到PostgreSQL

該函數獲取開始和結束日期並創建日期範圍並生成輸出。

CREATE FUNCTION [dbo].[DateRange] 
(  
     @Increment    CHAR(1), 
     @StartDate    DATETIME, 
     @EndDate    DATETIME 
) 
RETURNS 
@SelectedRange TABLE 
(IndividualDate DATETIME) 
AS 
BEGIN 
     ;WITH cteRange (DateRange) AS (
      SELECT @StartDate 
      UNION ALL 
      SELECT 
        CASE 
         WHEN @Increment = 'd' THEN DATEADD(dd, 1, DateRange) 
         WHEN @Increment = 'w' THEN DATEADD(ww, 1, DateRange) 
         WHEN @Increment = 'm' THEN DATEADD(mm, 1, DateRange) 
        END 
      FROM cteRange 
      WHERE DateRange <= 
        CASE 
         WHEN @Increment = 'd' THEN DATEADD(dd, -1, @EndDate) 
         WHEN @Increment = 'w' THEN DATEADD(ww, -1, @EndDate) 
         WHEN @Increment = 'm' THEN DATEADD(mm, -1, @EndDate) 
        END) 

     INSERT INTO @SelectedRange (IndividualDate) 
     SELECT DateRange 
     FROM cteRange 
     OPTION (MAXRECURSION 3660); 
     RETURN 
END 
GO 
+0

這個網站是編程問題。它不是代碼翻譯服務。 –

回答