Order against two columns at the same time (intersecting)

I have a table with the CommonName and FirstName fields. Only each field has data, but not both. Is there a way to arrange rows in a crossover way on SQL Server?

Example:

 CommonName FirstName Bern Wade Ashley Boris Ayana 

I want the entries to be arranged as follows:

 CommonName FirstName Ashley Ayana Bern Boris Wade 

Is this possible, and if so, how?

+1
source share
4 answers

Use the CASE statement to select the value for this row and ORDER BY , which.

+2
source

ORDER BY CommonName + FirstName , with corresponding ISNULL(<column>, '') if they are null.

+1
source
 ORDER BY CASE WHEN CommonName is null THEN FirstName ELSE CommonName END 
+1
source
 order by coalesce(CommonName, FirstName) 
0
source

Source: https://habr.com/ru/post/1333088/


All Articles