I am looking for a way to return NULL values last when sorting either DESC or ASC in SQL (the envelopping code is in Python) and maybe be able to retun the NULL values first depending on some config
I tried replacing the NULL values with numeric values but in the long run I found that it is not a suitable solution since in some cases I need to traceback the origin of the NULL, why I have it and putting a numeric value could influence the work of someone else and give inaccurate results.
if request.sort:
sql += " order by " + ', '.join([sort.split(':')[0] + " " + sort.split(':')[1] for sort in request.sort])
return sql
Alternate way on your implementation use union all
between filtered data
.... from table_name where column is not null order by column <desc/asc>
UNION ALL
.... from table_name where column is null
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments