truncate table sxc_itnrw_sfmk;

insert into sxc_itnrw_sfmk(
standort_nr,
       hochschulstandort,
       gueltig_ab_semester,
       gueltig_bis_semester,
       studienfach_land,
       studienfach_name,
       abschluss_land,
       stufrm,
       abschluss_name,
       stufrm_name,
       studientyp,
       studientyp_str,
       regelstudienzeit,
       lehreinheit,
       studienfach_hochschule_name
       )
       select hochschulnummer,
       hochschulstandort,
       val('' || gueltig_ab_jahr || gueltig_ab_semester) as gueltig_ab_semester,
       val('' || gueltig_bis_jahr || gueltig_bis_semester) as gueltig_bis_semester,
       studienfachschluessel_hsf,
       studienfach,
       substring(abschlussschluessel from 2 for 2),
       substring(abschlussschluessel from 1 for 1) as stufrm,
       abschlusspruefung,
       art_des_studiums,
       vtd_schluessel,
       studientyp,
       regelstudienzeit,
       lehreinheit,
       studienfach_hochschule
from sxc_itnrw_sfmk_neu
 ;
--führende 0en entfernen:
-- update sxc_itnrw_sfmk set hs_nr=substring(hs_nr from 2 for 10)
-- where hs_nr like '0%';
-- 
-- update sxc_itnrw_sfmk set hs_nr=substring(hs_nr from 2 for 10)
-- where hs_nr like '0%';


update sxc_itnrw_sfmk set standort_nr=(select K.hs_nr_kenn
from sxc_trans_hs_nr K
where K.hs_nr_sourcesystem=sxc_itnrw_sfmk.standort_nr)
where exists (select K.hs_nr_kenn
from sxc_trans_hs_nr K
where K.hs_nr_sourcesystem=sxc_itnrw_sfmk.standort_nr);

update sxc_itnrw_sfmk set hs_nr= standort_nr;
update sxc_itnrw_sfmk set hs_nr=  (select hs_nr from sxc_hochschulen_standorte where  standort_nr= sxc_itnrw_sfmk.standort_nr)
where standort_nr in (select standort_nr from sxc_hochschulen_standorte where hauptsitz is not null);

update sxc_itnrw_sfmk set gueltig_ab_semester=19001
where gueltig_ab_semester is null;


update sxc_itnrw_sfmk set gueltig_bis_semester=30001
where gueltig_bis_semester is null;



