title: How to convert this T-SQL code to PostgreSQL / plpgsql
body: <p>I need to convert the following tsql function code into a plpgsql function and I have absolutely no idea how:</p>
  
  <pre><code>BEGIN
      DECLARE @StartDate DATETIME
      DECLARE @ResultDate DATETIME
  
      SET @StartDate = CONVERT(DATETIME, 0)
  
      SET @ResultDate = 
      CASE @Type
          WHEN 0 THEN DATEADD(mi, FLOOR(DATEDIFF(mi, @StartDate, @Date) / CAST(@Interval AS FLOAT)) * @Interval, @StartDate)
          WHEN 1 THEN DATEADD(mi, CEILING(DATEDIFF(mi, @StartDate, @Date) / CAST(@Interval AS FLOAT)) * @Interval, @StartDate)
          ELSE @Date
  END
  
  RETURN @ResultDate
  </code></pre>
  
  <p>Here is the fullquote:</p>
  
  <pre><code>set ANSI_NULLS ON
  set QUOTED_IDENTIFIER ON
  GO
  
  ALTER FUNCTION [dbo].[GetIntervalDate] 
  
  (
      @Date DATETIME,
      @Type INT,
      @Interval INT
  )  
  RETURNS DATETIME
  
  AS
  
  BEGIN
  
  DECLARE @StartDate DATETIME
  
  DECLARE @ResultDate DATETIME
  
      SET @StartDate = CONVERT(DATETIME, 0)
  
      SET @ResultDate = 
      CASE @Type
          WHEN 0 THEN DATEADD(mi, FLOOR(DATEDIFF(mi, @StartDate, @Date) / CAST(@Interval AS FLOAT)) * @Interval, @StartDate)
          WHEN 1 THEN DATEADD(mi, CEILING(DATEDIFF(mi, @StartDate, @Date) / CAST(@Interval AS FLOAT)) * @Interval, @StartDate)
          ELSE @Date
      END
  
      RETURN @ResultDate
  END
  </code></pre>