I would like to ask you about this simple ORDER BY (in T-SQL) clause.
Let’s say we have these varchars in a table. (the table has ID, name, other columns)
'Peter'
''
''
'Anna'
'Michael'
'Bert'
''
''
''
The goal is to order them in ascending order, but so that the whitespaces will follow and not proceed these names. (I know that it sounds a little bit illogical, but I need all the datarows – including whitespaces – the table has in reality more columns and I cannot change anything). Of course, the code of these chars precede the alphabet, but how to trick it to produce this output?
SELECT name FROM table ORDER BY name
'Anna'
'Bert'
'Michael'
'Peter'
''
''
''
''
''
Thank you !
You can use
CASEinORDER BY:This query will first split all into two groups:
Then it will order by the first group first, then by the name itself. Then it’ll append the group without name.