The Sun, like other stars, will not physically leave the solar system. Instead, its evolution will lead to a phase known as the "red giant" phase, which will significantly impact the inner planets, including Earth.
In approximately 5 billion years, the Sun will exhaust its hydrogen fuel in its core and start burning helium. During this phase, it will expand into a red giant, swelling in size and becoming much brighter. As a red giant, the Sun will engulf Mercury and Venus, and it's possible that Earth may also be swallowed or affected by the Sun's outer layers.
The timescale for this process is quite long, spanning millions to billions of years. The exact duration of the red giant phase depends on various factors, including the mass of the star. However, the red giant phase itself is relatively short compared to the overall lifespan of a star.
It is important to note that this event has not occurred in Earth's history, nor has it happened in the history of our solar system. The Sun is currently in the main sequence phase, where it has been for approximately 4.6 billion years, and it will remain in this phase for several billion more years before evolving into a red giant.
As for the history of other stars and their potential red giant phases, it is difficult to determine with certainty when and how many times such events have occurred. Studying the evolution of stars in the universe is a complex task, and our knowledge is limited to what we can observe and infer from available data.