Selling an AFS security
On sale, the realized gain or loss in net income is proceeds minus amortized cost. Whatever unrealized gain or loss sits in AOCI for that security is removed through a reclassification adjustment, so the gain isn't counted twice. For a trading security, the sale gain is proceeds minus the last fair value, because earlier changes are already in net income.