At the end of The Lord Of The Rings: Two Towers, Sam puts the Ring on to hide from orcs and then follow them, and afterwards passes out.
Now the flicker of approaching torches and the clink of steel ahead was very near. In a minute they would reach the top and be on him. He had taken too long in making up his mind, and now it was no good. How could he escape, or save himself, or save the Ring? The Ring. He was not aware of any thought or decision. He simply found himself drawing out the chain and taking the Ring in his hand. The head of the orc-company appeared in the Cleft right before him. Then he put it on.
The great doors slammed to. Boom. The bars of iron fell into place inside. Clang. The gate was shut. Sam hurled himself against the bolted brazen plates and fell senseless to the ground. He was out in the darkness. Frodo was alive but taken by the Enemy.
The Lord Of The Rings: Two Towers, Book IV, Chapter 10, The Choices of Master Samwise
I didn't find any mention of him taking the Ring off, but then, in The Return of the King
There he halted and sat down. For the moment he could drive himself no further. He felt that if once he went beyond the crown of the pass and took one step veritably down into the land of Mordor, that step would be irrevocable. He could never come back. Without any clear purpose he drew out the Ring and put it on again. Immediately he felt the great burden of its weight, and felt afresh, but now more strong and urgent than ever, the malice of the Eye of Mordor, searching, trying to pierce the shadows that it had made for its own defence, but which now hindered it in its unquiet and doubt.
The Lord Of The Rings: The Return of the King, Book VI, Chapter 1, The Tower of Cirith Ungol
That supposes Sam was not wearing the Ring at the time.
So, when exactly did he take the Ring off? Was it before or after he was knocked unconscious?