A judge has halted a controversial ban on large sugary drinks in New York just hours before it was to go into force, ruling it "arbitrary and capricious".

The decision dealt a blow to the mayor of New York City, Michael Bloomberg, who had personally promoted the new restrictions. He insisted that his administration had the authority to implement the measure, due to go into effect on Tuesday, and promised to lodge an appeal.

The new rules, banning the sale of sugary drinks larger than 16oz (473ml), were the first attempt in the US to tackle rising obesity levels by placing legal limits on portion sizes.

But the restrictions were bitterly opposed by the food and drinks industry, which said they were riddled with loopholes and unenforceable.

The new rule banned the sale of large containers of sweetened drinks at food carts, restaurants, bowling alleys and movie theatres. But convenience shops and grocery stores, which lie beyond the authority of the city's health department, would still have been allowed to sell large containers. This inconsistency – and confusion about precisely which drinks were covered – raised the prospect that a branch of 7-Eleven could continue to sell a "Big Gulp" on the same block as a restaurant where such a drink would be banned.

In his 37-page ruling, judge Milton Tingling said the ban was "fraught with arbitrary and capricious consequences" that made the task of enforcing it almost impossible "even within a particular city block, much less the city as a whole".

He listed the many inconsistencies in the plan: that it did not apply to every food establishment in the city, that it excluded some drinks that had higher concentrations of sugar or calories "on suspect grounds", and that it did not limit the amount of refills. These served only to "gut the purpose of the rule", the judge said.

He went on: "The simple reading of the rule leads to the earlier acknowledged uneven enforcement even within a particular city block, much less the city as a whole … the loopholes in this rule effectively defeat the stated purpose of the rule."

The judge also ruled that pursuing the ban through the city's board of health, rather than through a vote on the city council, amounted to an over-reach of his executive powers.

The court ruling ensures the issue will be tied up in months of legal wrangling and raises a question over whether the restrictions will ever be introduced: Bloomberg's term ends in December and the leading candidate to replace him, Christine Quinn, has declared her opposition to the soda restrictions.

Michael Cardozo, counsel for New York City's law department, issued a statement saying the mayor's office would appeal, "and we are confident the board of health's decision will ultimately be upheld".

"This measure is part of the city's multi-pronged effort to combat the growing obesity epidemic, which takes the lives of more than 5,000 New Yorkers every year, and we believe the board of health has the legal authority – and responsibility – to tackle its leading causes."

The American Beverage Association, one of seven petitioners against the sugary drinks ban, said the ruling "provides a sigh of relief to New Yorkers and thousands of small businesses in New York City that would have been harmed by this arbitrary and unpopular ban".

"With this ruling behind us, we look forward to collaborating with city leaders on solutions that will have a meaningful and lasting impact on the people of New York City."

In a press conference at city hall in Manhattan, Bloomberg said the judge's decision "was clearly in error" and said the mayor's office "will prevail on appeal".

Bloomberg compared the ban on sugary drinks to his earlier public health initiatives, the clampdown on smoking in public and the rules that apply hygiene grades to restaurants. "Over the past 10 years we have adopted many groundbreaking and controversial public health policies," he said. Those and other measures "have helped New Yorkers live longer lives".

But he said the obesity crisis still had to be addressed. "The best science tells us that sugary drinks are a leading cause of obesity," Bloomberg said.

Taking aim at the soft drinks lobby, he said: "When you adopt a groundbreaking policy, special interest will sue. That's America."