I held a residence permit for work in Germany for a period of a bit more than 2 years, during which I was employed for a period of approx. 1.5 years, then I became self-employed for approx 6 months. I then switched to a Student visa to complete my Bachelor's degree, and have been working as a working student ever since. All in all I will complete 5 years in Germany in January 2022. My question is, will the time that I already spent as a worker in the country before becoming a student count towards permanent residency?
Yes, the time of work before you become a student will be taken into account. It is not about working several months/years in a row; it is the total sum of months/years you have paid into the social security system what is important. So a gap of beeing a student isnt a problem. Having graduaded in Germany will shorten the time down to 2 years to become eligable for permanent residency.