views:

140

answers:

1

Hi, ne need to convert the following tsql function code into a pgsql function and I have absolutely no idea how:

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

thanks

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

Sorry, here is the fullquote

+1  A: 

Got it:

  RETURNS timestamp with time zone AS $BODY$
        DECLARE
                _mystamp timestamp;
                _round_secs decimal;
        BEGIN       _round_secs := "@Interval"::decimal;

        IF "@Type" = 0    THEN
        RETURN timestamptz 'epoch' + FLOOR((EXTRACT(EPOCH FROM "@Date"))::int / _round_secs) *
_round_secs * INTERVAL '1 second';  ELSIF "@Type" = 1     THEN  
        RETURN timestamptz 'epoch' + CEIL((EXTRACT(EPOCH FROM "@Date"))::int / _round_secs) *
_round_secs * INTERVAL '1 second';    ELSE
       RETURN "@Date";  END IF;      END; $BODY$   LANGUAGE 'plpgsql' IMMUTABLE

Maybe anyone else needs something like this.

dbu
+1 for answering your own question.
Bob Jarvis
You only need those double quotes around it all because you did the whole @Uppercase thing. Drop the case to lower and drop the quotes - its alot more normal plpgsql then.
rfusca