Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql to mssql "order by"

Status
Not open for further replies.

iwanalearn

Technical User
Sep 21, 2001
22
US
I have a database that searches for dealers based on the zip code entered. I want the results to show the dealers with the same zip code on top of the list then alphabetical after that. We have switched from mysql to mssql. Learning mssql but need help converting this code.

This is how I sorted the results in mysql.
$result = mysql_query
("SELECT * FROM zipcodes, dealers
WHERE zipcodes.Start <='$zip' and zipcodes.Stop >='$zip'
and dealers.DealerID=zipcodes.DealerId
ORDER BY dealers.Zip='$zip'DESC, dealers.DealerName ASC
LIMIT 10");

This is where I am with the mssql.
$result = mssql_query
("SELECT TOP 10 * FROM zipcodes, dealers
WHERE zipcodes.Start <='$zip' and zipcodes.Stop >='$zip'
and dealers.DealerID=zipcodes.DealerId
order by dealers.DealerName ASC");
 
SQL Server is a bit more strict about implicit boolean conversions. Try:

Code:
order by case when dealers.Zip='$zip' then 1 else 0 end DESC, dealers.DealerName ASC

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top