case when statement for order by clause in sql server

It is great feature where can when can be used in order by clause in sql server , below is the query where at the time of creation record modified date will be null and creation date will be present date time, but at the time of update modified date will be updated, now using order by clause and case when ,

SELECT [incident_IncidentId]
,[incident_CaseNum]
,[incident_CreatedDate]
,[incident_ModifiedDate]
,[incident_StatusId]
,[incident_NoOfVictim]
,[incident_Note]
FROM [PCCAS].[dbo].[incident] a

ORDER BY

CASE WHEN a.incident_ModifiedDate IS NULL THEN a.incident_CreatedDate
ELSE a.incident_ModifiedDate END DESC

casewhenorderby

Now, datawill come based on case when

CATEGORIES:

Tags:

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *