By Annie Gasparro

Kraft Heinz Co. wrote down the value of Oscar Mayer, Maxwell House and several other of its well-known brands, reflecting the challenges for the food maker despite strong sales in recent months amid the pandemic.

Kraft Heinz on Thursday recorded $2.9 billion in impairment charges, which resulted in the company swinging to a loss in the second quarter. The hit comes after Kraft Heinz last year reduced the value of its assets by nearly $17 billion. It said at the time that consumers had gravitated toward niche brands often viewed as fresher, healthier, or more innovative.

Kraft Heinz Chief Executive Miguel Patricio said he thinks Oscar Mayer hot dogs and cold cuts have potential for sales growth. But he expects lower profit margins due to investments in the brand and competitive pricing, leading to $626 million in write-downs tied to the brand.

"Protein is one of the big trends. We want to grow this brand. We are going to invest more in it," Mr. Patricio said.

The company said another $655 million of the latest charges were related to its U.S. food service business, which could have slower sales growth going forward as the pandemic slashes demand at restaurants, schools and hotels.

When a company finds that the value of an asset is lower than it has been accounting for on its balance sheet, it reflects the loss through a write-down on its earnings.

Kraft Heinz has been in the midst of a yearslong turnaround effort. The coronavirus pandemic accelerated its progress as shoppers returned to big, familiar grocery brands, Mr. Patricio said.

"We have to keep these consumers with us," he said.

Kraft Heinz's results topped analyst expectations for quarter when excluding the impact of the charges and other factors that affect comparability. Its shares fell 5% Thursday to $33.77.

The company said its comparable sales -- -- which strips out the effects of currency fluctuations, acquisitions and divestitures -- -- rose 7.4% in the second quarter. "This is not to say that we captured 100% of the opportunity. There are some categories where we have lost share, and we are working hard to fix that," Mr. Patricio said.

Kellogg Co. also reported better-than-expected earnings Thursday, with comparable sales up 9% in the second quarter. Kellogg's shares were flat at $70.76.

"People just aren't on the go as much. They're eating more at home. We have to make that stick," Kellogg Chief Executive Steve Cahillane said.

Kellogg's North America retail sales increased more than 15% in cereal after tepid sales for years, and 26% for its Eggo frozen breakfast items.

Food makers initially saw an unprecedented surge in sales in March and April when consumers filled up their pantries and refrigerators amid lockdowns to curtail the spread of the coronavirus. Lately, consumers aren't stocking up as much anymore, but they are still grocery shopping more than usual, food makers said.

Kraft Heinz and Kellogg said they have increased their production capacity and continue to sell essentially everything they can make.

Snack maker Mondelez International Inc. earlier this week reported strong sales growth in North American, saying its biggest brands such as Oreo cookies have gained market share.

"Consumers have been going back to the brands they know and trust," Mondelez CEO Dirk Van de Put said.

Nestlé SA said Thursday sales slowed recently, though the maker of Lean Cuisine and DiGiorno pizza reported comparable sales were up 1.3% in the second quarter.

Kraft Heinz and Kellogg said they are increasing advertising for the rest of the year in hopes of keeping their sales momentum going beyond the pandemic.

"We have been investing a lot of time in understanding who these new consumers are and what they like," Kraft Heinz's Mr. Patricio said.

Kellogg said it is spending more on smaller brands that didn't get much investment before, such as Corn Pops and Corn Flakes cereal in the U.S.

Overall for the quarter, Kraft Heinz reported a second-quarter loss of $1.65 billion, compared with a profit of $449 million a year earlier. Excluding the impairment charges and other items, its adjusted profit rose to 80 cents a share. Its revenue rose 3.8% from the prior year to $6.65 billion.

Kellogg reported earnings of $351 million, up from $286 million a year earlier. The company's adjusted profit, accounting for currency fluctuations, rose to $1.26 a share, and sales were roughly flat at $3.47 billion compared with last year.

Micah Maidenberg contributed to this article.

Write to Annie Gasparro at annie.gasparro@wsj.com

Corrections & Amplifications

This item was corrected at 7:25 p.m. ET to show that Kraft Heinz Co.'s revenue rose 3.8% in the second quarter, not 3.6%.