(Reuters) - Target Corp (>> Target Corporation) on Wednesday reported an increase in second-quarter comparable-store sales after four straight negative quarters, driven by improved online traffic and demand across all businesses except groceries.

Target shares rose as much as 4 percent after the retailer beat second-quarter profit and same-store sales expectations, traffic jumped 2.1 percent, and it raised its full-year adjusted earnings forecast.

Chief Executive Officer Brian Cornell said comparable sales rose across all Target's categories during the quarter, except food and beverage, which was flat, an improvement from the decline in the first quarter.

"This was one of the top-performing retailers over the last decade. For a little while, it seemed to have lost its way but now appears to be getting back into that 'Tar-jay' mode from both a merchandising and an operating perspective," Moody's analyst Charlie O’Shea said, referring to the faux-French pronunciation favored by customers.

Like other retailers, Target has struggled to boost traffic amid changing consumer habits and competition from e-commerce giant Amazon.com Inc (>> Amazon.com).

Target shares have slid 25 percent since the start of the year, widely lagging larger rival Wal-Mart (>> Wal-Mart Stores), whose stock has risen 17 percent.

In its latest turnaround bid, Target vowed this year to double the number of small-format stores, invest heavily in e-commerce, aggressively promote its products and keep grocery prices low to compete with Wal-Mart, Amazon and supermarket chain Kroger Co (>> Kroger Co).

"Probably, the best bang for Target's buck is a combination of investment in online and grocery," O’Shea said, adding that Target's rapidly growing online sales are still only a small fraction of overall revenue.

"Food is a very important category. It drives traffic and traffic begets sales of exclusive brands. A lot of Wal-Mart's success has been the explosion of its food business."

Comparable online sales soared 32 percent in the quarter ended July 29, compared with last year's 16 percent increase. This helped total comparable sales to rise 1.3 percent, beating the 0.7 percent growth expected by analysts polled by research firm Consensus Metrix.

Lower pricing and promotions hurt Target's gross margin rate by about 40 basis points, Chief Financial Officer Cathy Smith said on a post-earnings conference call, and could face additional margin pressure for the rest of the year.

Excluding items, the Minneapolis-based retailer earned $1.23 per share in the second quarter, beating the average analyst estimate of $1.19, according to Thomson Reuters I/B/E/S. Sales rose 1.6 percent to $16.43 billion, above the average analyst estimate of $16.30 billion.

Target raised its full-year adjusted earnings forecast to $4.34-$4.54 per share from $3.80-$4.20.

The company's stock was up 3.2 percent to $56.07 at midday after rising to $56.46.

(Reporting by Richa Naidu in Chicago and Sruthi Ramakrishnan in Bengaluru; Editing by Martina D'Couto and Jeffrey Benkoe)

By Richa Naidu and Sruthi Ramakrishnan

Stocks treated in this article : Wal-Mart Stores, Target Corporation, Kroger Co, Amazon.com