The given sentence indicates an action that began in the past and completed before another action commenced. In the given sentence, the action (owning a car) completed before another commenced (I was sad when I sold my car.) So, the tense will be past perfect.
The structure used in this tense is: had + past participle
From the options, 'had had' is the most suitable one. It follows the structure had + past participle (had).
'Had had' is a double word, in which both words do not mean the same thing. The first 'had' is the helping verb that indicates the tense of the sentence.
The second 'had' is the main verb that has a different meaning: 'to own'.
The sentence means: I had owned it for a very long time.
Hence option B is correct.
Option A: 'Have had' is in the present perfect tense. It indicates an action that has just ended in the present. But the words 'when I sold my car' refer to an action that completed in the past. Hence A is incorrect.
Option C: 'Has had' is incorrect because 'has' is used in third person whereas 'I' is first person.
Option D: 'Is having' is also incorrect because 'is' refers to third person. Also, 'having' refers to a present continuous action, whereas the action here is in the past.