So Oracle has NULLS FIRST, which I can use to have null values sorted at the top followed by my column value in descending order:
ORDER BY date_sent NULLS FIRST
What is comparable in SQL Server? There are these alternatives, assuming the date values are NULL or in the past:
ORDER BY ISNULL(date_sent, GETDATE()) DESC ORDER BY (CASE WHEN t.setinactive IS NULL THEN 1 ELSE 2 END), t.setinactive DESC ORDER BY -CAST(date_sent as int) ASC
Any others?
ISNULL replaced the Oracle NVL function in the SQL server. When an expression in SQL server is NULL, the ISNULL function allows you to return an alternative value for the null.
If you sort a column with NULL values in ascending order, the NULLs will come first. Alternatively, if you add a DESC keyword to get a descending order, NULLs will appear last.
UPDATE [table] SET [column]=0 WHERE [column] IS NULL; Null Values can be replaced in SQL by using UPDATE, SET, and WHERE to search a column in a table for nulls and replace them. In the example above it replaces them with 0.
SQL Server ISNULL() FunctionThe ISNULL() function returns a specified value if the expression is NULL. If the expression is NOT NULL, this function returns the expression.
You can do some trick:
ORDER BY (CASE WHEN [Order] IS NULL THEN 0 ELSE 1 END), [Order]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With