sql - Refactor using COALESCE, ISNULL or CASE WHEN -


i have following sql query

go select @startdate= table1.startdate, @enddate= table2.enddate table1 join table2  select @startdate= table2.startdate, @enddate= table3.enddate table2 join table3  select @startdate= table3.startdate, @enddate= table4.enddate table3 join table4 

basically, if last query not null final value of @startdate , @enddate. best approach on this, thinking of using isnull, coalesce or case when

i think there no need 3 queries desired result. isnull function evaluated once. coalesce expression can evaluated multiple times.

  select @startdate = coalesce(table3.startdate, table2.startdate, table1.startdate)         ,@enddate = coalesce(table4.enddate, table3.enddate, table2.enddate)     table1     left join table2     left join table3     left join table4 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -