   select 
     co.subject_id
     , co.stay_id
-    , GREATEST(min(charttime - interval '1' hour), co.starttime) as starttime
-    , LEAST(max(charttime + interval '4' hour),co.endtime) as endtime
-    , max(case when lower(ce.value) similar to '%(cpap mask|bipap mask)%' then 1 else 0 end) as cpap
+    , GREATEST(min(DATETIME_SUB(charttime, INTERVAL '1' HOUR)), co.starttime) as starttime
+    , LEAST(max(DATETIME_ADD(charttime, INTERVAL '4' HOUR)), co.endtime) as endtime
+    , max(case when REGEXP_CONTAINS(lower(ce.value), '(cpap mask|bipap)') then 1 else 0 end) as cpap
   from co
   inner join `physionet-data.mimic_icu.chartevents` ce
     on co.stay_id = ce.stay_id
     and ce.charttime > co.starttime
     and ce.charttime <= co.endtime
   where ce.itemid = 226732
-  and lower(ce.value) similar to '%(cpap mask|bipap mask)%'
+  and REGEXP_CONTAINS(lower(ce.value), '(cpap mask|bipap)')
   group by co.subject_id, co.stay_id, co.starttime,co.endtime
 )
 

    AND bg.charttime > co.starttime
    AND bg.charttime <= co.endtime
  left join `physionet-data.mimic_derived.ventilation` vd
    on bg.subject_id = vd.subject_id
    and bg.charttime > vd.starttime
    and bg.charttime <= vd.endtime
    and vd.ventilation_status = 'InvasiveVent'

-- icd-9 diagnostic codes are our best source for comorbidity information
-- unfortunately, they are technically a-causal
-- however, this shouldn't matter too much for the SAPS II comorbidities
, comorb as
(
select hadm_id
