title: TRANSACT SQL to PL/pgSQL
body: <p>I have a lot of functions and stored procedures to export from MS SQL server to PostgreSQL </p>
  
  <p>Can you please help me with this function and giving me the equivalent function in PL/pgSQL for PostgreSQL?</p>
  
  <pre><code>USE [METRICS]
  GO
  
  SET ANSI_NULLS ON
  GO
  SET QUOTED_IDENTIFIER ON
  GO
  
  ALTER FUNCTION [dbo].[fnPSR_CtrlNouveauProjet]
  (
      @InstanceID int,
      @Month_Param nvarchar(15)
  )
  RETURNS nvarchar(2)
  AS
  BEGIN
      DECLARE @ReturnValue NVARCHAR(2), @Max_ID_Token int
      DECLARE @Month_Param_Prev nvarchar(15)
      SELECT @Month_Param_Prev = CASE WHEN @Month_Param like '%-08' THEN  SUBSTRING(CONVERT(VARCHAR(10), DATEADD(MONTH, DATEDIFF(MONTH, 0, '01-'+@Month_Param) - 2, 0), 105),4,10) 
           ELSE  SUBSTRING(CONVERT(VARCHAR(10), DATEADD(MONTH, DATEDIFF(MONTH, 0, '01-'+@Month_Param) - 1, 0), 105),4,10) end 
      DECLARE @KIS_FileName NVARCHAR(100)
      SELECT 
          @KIS_FileName = KI.KIS_FileName 
      FROM 
          PSR_KIS_INSTANCE KI (NOLOCK)
      WHERE
          KI.ID_Instance = @InstanceID
  
      SELECT @Max_ID_Token = MAX(KI.ID_Token) FROM DBO.PSR_KIS_INSTANCE KI (NOLOCK) WHERE KI.KIS_FileName = @KIS_FileName and KI.Reporting_Month = @Month_Param_Prev
  
      IF (EXISTS(SELECT 
          KI.ID_Instance 
      FROM 
          PSR_KIS_INSTANCE KI (NOLOCK) 
      WHERE 
          KI.KIS_FileName  = @KIS_FileName
          and KI.Reporting_Month = @Month_Param_Prev 
          and KI.ID_Token = @Max_ID_Token))
  
      BEGIN
          SET @ReturnValue = ' '
      END
      ELSE
      BEGIN
          SET @ReturnValue = 'N'
      END
  
      RETURN @ReturnValue
  
  END
  </code></pre>