user2571510
user2571510

Reputation: 11377

Proper way to order by name in SQL Server

I have a query containing lines like the following:

SELECT    B.LastName + ', ' + B.FirstName AS TM

Currently I use ORDER BY TM in order to sort, which seems to work.

However, in order to do it right, can someone tell me what is the correct way here to order by TM?

ORDER BY TM,  
ORDER BY B.TM,  
ORDER BY B.LastName + ', ' + B.FirstName 

Or something else?

Also, can you tell me if I need to put the TM in single quotes?

It seems to work without the quotes but I want to make sure I do this right.

Upvotes: 0

Views: 59

Answers (1)

jarlh
jarlh

Reputation: 44776

ORDER BY TM

Best way!

ORDER BY B.TM,

No, TM is not one of B's columns.

ORDER BY B.LastName + ', ' + B.FirstName

Can be done, but first alternative is so much easier.

Qutes are required for reserved words, and for identifiers including strange characters (incl space).

Upvotes: 1

Related Questions