Stocks rallied for a second straight day on Thursday, taking the Nasdaq back into positive territory for the year-to-date, on increasing optimism that the economy's worst days are behind after the government reported data that was less dire than expected.

Electronics retailer Best Buy reported results and an outlook that topped market estimates, reinforcing views of rising consumer demand and driving up other retailers. Best Buy shares rose 12.6 percent.

Big cap technology stocks pushed the Nasdaq into positive territory for the year, with Apple Inc leading the charge with a gain of 3.2 percent. Analysts say technology could outperform other sectors as companies spend on capital projects when they sense a turnaround.

Leading discount retailer Wal-Mart Stores Inc was among the top boosts on the Dow, rising more than 2 percent to $52.76, while the S&P retail index <.RLX> gained 4.4 percent.

Obviously the tide is shifting. We've gone from every piece of news being incrementally bad to not as bad as expectations, said Stephanie Giroux, chief investment strategist at TD Ameritrade in Jersey City, New Jersey. The fact that collectively we are starting to see things less negative is very significant.

The Dow Jones industrial average <.DJI> jumped 174.75 points, or 2.25 percent, to 7,924.56. The Standard & Poor's 500 Index <.SPX> spiked 18.98 points, or 2.33 percent, to 832.86. The Nasdaq Composite Index <.IXIC> surged 58.05 points, or 3.80 percent, to 1,587.00.

Coupled with earlier data on housing and durable goods this week that showed signs of life in the economy, investors bet on an improving outlook, taking the S&P 500 up 23.1 percent since it hit a 12-year low on March 9.

At the current pace, the S&P 500 could have its biggest monthly gain in 22 years.

Stocks rose even as government data showed the U.S. economy contracted at its fastest pace since 1982 in the fourth quarter, but the decline was slightly less than expected. Corporate profits in the fourth quarter plunged by the biggest margin since 1994. The number of workers collecting state unemployment benefits rose to a record high in the latest week.

Two of the worst performers this year, homebuilder and bank stocks, extended gains on Thursday. The Dow Jones homebuilders index <.DJUSHB> rose 7.0 percent. Luxury homebuilder Toll Brothers Inc rose 4.3 percent to $19.96, while D.R. Horton Inc , the biggest U.S. homebuilder, jumped 8.7 percent to $10.93.

The S&P's technology index <.GSPT> and the materials index <.GSPM> both finished higher and are now positive for the year to date.

Shares of natural resources companies rose along with higher commodity prices on Thursday. Shares of steel maker Nucor rose over 5 percent to $41.09 and U.S. Steel Corp was up 5.4 percent to $24.74.

Investors were relieved to see fair demand for new supply of $24 billion of U.S. government debt after a poor auction a day earlier raised fears the government would have trouble funding its plans to help the economy recover.

On Nasdaq shares of Apple, maker of the iPhone, jumped 3.2 percent to $109.87.

Goldman Sachs reiterated a buy rating on Research In Motion Ltd , saying it expects the BlackBerry maker to post fourth-quarter results in line with Wall Street's expectations. The stock jumped 4.9 percent to $45.04

The semiconductor index <.SOXX> rose 6 percent.

(Editing by Leslie Adler)