   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
 )
 

    co.stay_id
  , bg.charttime
  , pao2fio2ratio AS PaO2FiO2
  , case when vd.subject_id is not null then 1 else 0 end as vent
  , case when cp.subject_id is not null then 1 else 0 end as cpap
  from co
  LEFT JOIN `physionet-data.mimic_derived.bg` bg

        , hadm_id
        , stay_id
        , intime AS starttime
        , DATETIME_ADD(intime, INTERVAL '24' HOUR) AS endtime
    from `physionet-data.mimic_icu.icustays` ie
)
, cpap as
