Al-Ilmam bi-Ahadith al-Ahkam 423
عَن أنس رَضِيَ اللَّهُ عَنْه عَن النَّبِي ﷺ (أَنه) قَالَ: لَا يتمنّين أحدكُم الْمَوْت لضر نزل بِهِ، وَليقل: اللَّهُمَّ، أحيني مَا كَانَت الْحَيَاة خيرا لي، وتوفني إِذا كَانَت الْوَفَاة خيرا لي ". اتَّفقُوا عَلَى إِخْرَاجه، وَاللَّفْظ لِلتِّرْمِذِي.
From Anas, may Allah be pleased with him, from the Prophet ﷺ, (that) he said: "Let none of you wish for death because of some harm that has befallen him. Let him say: O Allah, keep me alive as long as life is better for me, and take me when death is better for me." They agreed on recording it, and the wording is at-Tirmidhi's.