BackgroundType 2 diabetes (T2D) is expected to worsen the prognosis of inpatients with heart failure (HF) but the evidence from observational studies is inconsistent. We aimed to compare mortality outcomes and life expectancy among inpatients with HF with or without T2D and explored whether chronic kidney disease (CKD) influenced these associations.MethodsWe collected hospital and civil registry records of consecutive inpatients from a tertiary hospital in Switzerland with a diagnosis of HF from the year 2015 to 2019. We evaluated the association of T2D with mortality risk using Cox regression and adjusted for confounders.ResultsOur final cohort consisted of 10,532 patients with HF of whom 27% had T2D. The median age (interquartile range [IQR]) was 75 [68 to 82] and 78 [68 to 86] for the diabetes and non-diabetes groups, respectively. Over a median follow-up [IQR] of 4.5 years [3.3 to 5.6], 5,347 (51%) of patients died. T2D patients had higher risk of all-cause mortality (hazard ratio [HR] 1.21, 95% confidence interval [CI] 1.14 to 1.29). Compared to control (i.e. no T2D nor CKD), average life expectancy (95% CI) among T2D patients, CKD, or both was shorter by 5.4 months (95% CI 1.1 to 9.7), 9.0 months (95% CI 8.4 to 9.6), or 14.8 months (95% CI 12.4 to 17.2), respectively. No difference by sex or ejection fraction category was observed.ConclusionsT2D is associated with a significantly higher risk of all-cause mortality and shorter life expectancy compared to those without among middle-aged and elderly inpatients with HF; presence of CKD may further increase these risks.Supplementary InformationThe online version contains supplementary material available at 10.1186/s12933-023-01903-7.