How can I avoid using two iSNulls

41 Views Asked by At

I am Trying to not use two isnulls since it is making my query extremely slow. what better ways are there to write the following?

[Example] =

CAST (isnull(ap.CustomDate2, (isnull(ap.CustomDate10,
                        (Select top 1 sts.times
                                From timetable sts
                                        where sts.ShipmentStatusID IN (8000, 8089)
                                        Order By sts.times DESC)))) AS date
                                      
1

There are 1 best solutions below

0
ClearlyClueless On

You would be better off using a COALESCE here. Assuming you are indeed working in Sql Server and not MySql

    CAST( 
        COALESCE(
            ap.CustomerDate2, 
            ap.CustomerDate10, 
            (SELECT TOP 1 sts.times
             FROM timetable sts
             WHERE sts.ShipmentStatusID IN (8000, 8089)
             ORDER BY sts.times DESC)
        )
     AS DATE) as Your_Column_Name