<table xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<row>
  <obj_type>FUNCTION</obj_type>
  <obj_name>nvl</obj_name>
  <obj_hashval>public_251223</obj_hashval>
  <obj_text>CREATE OR REPLACE FUNCTION public.nvl(p1 numeric, p2 text)
 RETURNS text
 LANGUAGE sql
AS $function$
  SELECT Coalesce(p1::text, p2);
$function$
</obj_text>
</row>

<row>
  <obj_type>FUNCTION</obj_type>
  <obj_name>last_day</obj_name>
  <obj_hashval>public_251223</obj_hashval>
  <obj_text>CREATE OR REPLACE FUNCTION public.last_day(fromdt anyelement)
 RETURNS date
 LANGUAGE sql
 IMMUTABLE STRICT
AS $function$
  SELECT (date_trunc('MONTH', cast(fromdt as date)) + INTERVAL '1 MONTH - 1 day')::DATE;
$function$
</obj_text>
</row>

<row>
  <obj_type>FUNCTION</obj_type>
  <obj_name>bitand</obj_name>
  <obj_hashval>public_251223</obj_hashval>
  <obj_text>CREATE OR REPLACE FUNCTION public.bitand(p1 numeric, p2 numeric)
 RETURNS numeric
 LANGUAGE sql
 STRICT
AS $function$
  SELECT (p1::INTEGER &amp; p2::INTEGER)::NUMERIC;
$function$
</obj_text>
</row>

<row>
  <obj_type>FUNCTION</obj_type>
  <obj_name>fnc_getcodfirma</obj_name>
  <obj_hashval>public_251223</obj_hashval>
  <obj_text>CREATE OR REPLACE FUNCTION public.fnc_getcodfirma()
 RETURNS numeric
 LANGUAGE sql
AS $function$
 SELECT TO_NUMBER((current_setting('PKG_GLOBALS.CODFIRMA',true)),'999999999999999999');;
 $function$
</obj_text>
</row>

<row>
  <obj_type>FUNCTION</obj_type>
  <obj_name>get_schema</obj_name>
  <obj_hashval>public_251223</obj_hashval>
  <obj_text>CREATE OR REPLACE FUNCTION public.get_schema()
 RETURNS character varying
 LANGUAGE sql
AS $function$
  SELECT n.nspname::varchar FROM pg_catalog.pg_namespace n WHERE n.nspname = ANY(current_schemas(FALSE)) AND n.nspname NOT IN ( 'public', 'winmadmin') LIMIT 1;
 $function$
</obj_text>
</row>

<row>
  <obj_type>FUNCTION</obj_type>
  <obj_name>get_temp_tablespace</obj_name>
  <obj_hashval>public_251223</obj_hashval>
  <obj_text>CREATE OR REPLACE FUNCTION public.get_temp_tablespace()
 RETURNS character varying
 LANGUAGE sql
AS $function$
  SELECT
    CASE
      WHEN (SELECT Count(1) FROM pg_catalog.pg_tablespace n WHERE n.spcname = 'winmdb_temp') = 0 THEN ''
      ELSE ' TABLESPACE winmdb_temp'
    END;
 $function$
</obj_text>
</row>

<row>
  <obj_type>FUNCTION</obj_type>
  <obj_name>instr</obj_name>
  <obj_hashval>public_251223</obj_hashval>
  <obj_text>CREATE OR REPLACE FUNCTION public.instr(str character varying, sub character varying, startpos integer DEFAULT 1, occurrence integer DEFAULT 1)
 RETURNS integer
 LANGUAGE plpgsql
AS $function$
declare
  tail  text;
  shift int;
  pos   int;
  i     int;
begin
    shift:= 0;
    if startpos = 0 or occurrence &lt;= 0 then
        return 0;
    end if;
    if startpos &lt; 0 then
        str:= reverse(str);
        sub:= reverse(sub);
        pos:= -startpos;
    else
        pos:= startpos;
    end if;
    for i in 1..occurrence loop
        shift:= shift+ pos;
        tail:= substr(str, shift);
        pos:= strpos(tail, sub);
        if pos = 0 then
            return 0;
        end if;
    end loop;
    if startpos &gt; 0 then
        return pos+ shift- 1;
    else
        return length(str)- pos- shift + 2;
    end if;
end $function$
</obj_text>
</row>

<row>
  <obj_type>FUNCTION</obj_type>
  <obj_name>instrb</obj_name>
  <obj_hashval>public_251223</obj_hashval>
  <obj_text>CREATE OR REPLACE FUNCTION public.instrb(str character varying, sub character varying, startpos integer DEFAULT 1, occurrence integer DEFAULT 1)
 RETURNS integer
 LANGUAGE plpgsql
AS $function$
declare
  tail  text;
  shift int;
  pos   int;
  i     int;
begin
    shift:= 0;
    if startpos = 0 or occurrence &lt;= 0 then
        return 0;
    end if;
    if startpos &lt; 0 then
        str:= reverse(str);
        sub:= reverse(sub);
        pos:= -startpos;
    else
        pos:= startpos;
    end if;
    for i in 1..occurrence loop
        shift:= shift+ pos;
        tail:= substr(str, shift);
        pos:= strpos(tail, sub);
        if pos = 0 then
            return 0;
        end if;
    end loop;
    if startpos &gt; 0 then
        return pos+ shift- 1;
    else
        return length(str)- pos- shift + 2;
    end if;
end $function$
</obj_text>
</row>

<row>
  <obj_type>FUNCTION</obj_type>
  <obj_name>nvl</obj_name>
  <obj_hashval>public_251223</obj_hashval>
  <obj_text>CREATE OR REPLACE FUNCTION public.nvl(p1 text, p2 text)
 RETURNS text
 LANGUAGE sql
AS $function$
  SELECT Coalesce(p1, p2);
$function$
</obj_text>
</row>

<row>
  <obj_type>FUNCTION</obj_type>
  <obj_name>nvl</obj_name>
  <obj_hashval>public_251223</obj_hashval>
  <obj_text>CREATE OR REPLACE FUNCTION public.nvl(p1 text, p2 numeric)
 RETURNS text
 LANGUAGE sql
AS $function$
  SELECT Coalesce(p1, p2::text);
$function$
</obj_text>
</row>

<row>
  <obj_type>FUNCTION</obj_type>
  <obj_name>nvl</obj_name>
  <obj_hashval>public_251223</obj_hashval>
  <obj_text>CREATE OR REPLACE FUNCTION public.nvl(p1 numeric, p2 numeric)
 RETURNS numeric
 LANGUAGE sql
AS $function$
  SELECT Coalesce(p1, p2);
$function$
</obj_text>
</row>

<row>
  <obj_type>FUNCTION</obj_type>
  <obj_name>to_char</obj_name>
  <obj_hashval>public_251223</obj_hashval>
  <obj_text>CREATE OR REPLACE FUNCTION public.to_char(fromdt anyelement)
 RETURNS text
 LANGUAGE sql
AS $function$
  SELECT fromdt::text;
$function$
</obj_text>
</row>

<row>
  <obj_type>FUNCTION</obj_type>
  <obj_name>to_char</obj_name>
  <obj_hashval>public_251223</obj_hashval>
  <obj_text>CREATE OR REPLACE FUNCTION public.to_char(pval numeric, pformat text, pnls text)
 RETURNS text
 LANGUAGE sql
AS $function$
  -- Stef 21.06.2022 -&gt; pentru pkg_exportedi
  SELECT
    CASE
      WHEN pNLS = 'NLS_NUMERIC_CHARACTERS=''.,''' THEN Replace(To_Char(pVal, pFormat), ',', '.')
      WHEN pNLS = 'NLS_NUMERIC_CHARACTERS='',.''' THEN Replace(To_Char(pVal, pFormat), '.', ',')
      ELSE To_Char(pVal, pFormat)
    END;
$function$
</obj_text>
</row>

<row>
  <obj_type>FUNCTION</obj_type>
  <obj_name>to_number</obj_name>
  <obj_hashval>public_251223</obj_hashval>
  <obj_text>CREATE OR REPLACE FUNCTION public.to_number(fromdt text)
 RETURNS numeric
 LANGUAGE sql
AS $function$
  SELECT fromdt::numeric;
$function$
</obj_text>
</row>

<row>
  <obj_type>FUNCTION</obj_type>
  <obj_name>trunc</obj_name>
  <obj_hashval>public_251223</obj_hashval>
  <obj_text>CREATE OR REPLACE FUNCTION public.trunc(pdate date)
 RETURNS date
 LANGUAGE sql
AS $function$
 SELECT pDate;
 $function$
</obj_text>
</row>

<row>
  <obj_type>FUNCTION</obj_type>
  <obj_name>trunc</obj_name>
  <obj_hashval>public_251223</obj_hashval>
  <obj_text>CREATE OR REPLACE FUNCTION public.trunc(pdate date, pformat character varying)
 RETURNS date
 LANGUAGE sql
AS $function$
 SELECT
   CASE
    WHEN NULLIF(pformat, '') IS NULL THEN pDate
    WHEN Upper(pformat) = 'DD'       THEN date_trunc('DAY',   CAST(pdate as date))::date
    WHEN Upper(pformat) = 'MM'       THEN date_trunc('MONTH', CAST(pdate as date))::date
    WHEN Upper(pformat) = 'YEAR'     THEN date_trunc('YEAR',  CAST(pdate as date))::date
    WHEN Upper(pformat) = 'IYYY'     THEN to_date('01.'||to_char(pdate, 'IYYY'), 'IW.IYYY')
    WHEN Upper(pformat) = 'IW'       THEN to_date(to_char(pdate, 'IW.IYYY'), 'IW.IYYY')
    ELSE pDate
   END;
 $function$
</obj_text>
</row>

<row>
  <obj_type>PROCEDURE</obj_type>
  <obj_name>set_locale_suport</obj_name>
  <obj_hashval>public_251223</obj_hashval>
  <obj_text>CREATE OR REPLACE PROCEDURE public.set_locale_suport()
 LANGUAGE plpgsql
AS $procedure$
BEGIN
  SET LC_MESSAGES="Romanian_Romania.1250";
  SET LC_NUMERIC ="Romanian_Romania.1250";
  SET LC_TIME    ="Romanian_Romania.1250";
  SET LC_MONETARY="Romanian_Romania.1250";
EXCEPTION WHEN OTHERS THEN
  SET LC_MESSAGES="ro_RO.UTF-8";
  SET LC_NUMERIC ="ro_RO.UTF-8";
  SET LC_TIME    ="ro_RO.UTF-8";
  SET LC_MONETARY="ro_RO.UTF-8";
END;
$procedure$
</obj_text>
</row>

</table>
