If the Ministry of Justice has ordered you to be transferred from prison to hospital under Section 47, at the same time it can also impose a 'restriction direction' on you under Section 49. This means that you can only be discharged, transfered and given leave from hospital with permission from the Secretary of State for Justice.
You can be kept in hospital until the end of the Section 47 or the date when you should be released from prison.