


insert into public.identity_principals(id,created_time,status,identity_user_id,last_updated_time)
select
  a.id,'2021-01-01',
  (case when COALESCE(b.LOCKFLAG,1)=0 or COALESCE(b.STATUS,1)=0 or COALESCE(b.DIMISSION,1)=0 then 2 else 0 end),
  a.id,
  a.created_time
from identity_user a join t_user b on a.id = b.ID and a.id not in (select id from identity_principals);



CREATE OR REPLACE FUNCTION SUBSTRING_INDEX(varchar, varchar, integer)
            RETURNS varchar AS $$
            DECLARE
            tokens varchar[];
length integer ;
indexnum integer;
BEGIN
  tokens := pg_catalog.string_to_array($1, $2);
length := pg_catalog.array_upper(tokens, 1);
indexnum := length - ($3 * -1) + 1;
IF $3 >= 0 THEN
RETURN pg_catalog.array_to_string(tokens[1:$3], $2);
ELSE
RETURN pg_catalog.array_to_string(tokens[indexnum:length], $2);
END IF;
END;
$$ IMMUTABLE STRICT LANGUAGE PLPGSQL;
