Before your brain explodes yes i have seen this.
My question is tailored to my specific situation. Im trying to optimize a very large database and im trying to rewrite some calls because it was converted from an Orical DB to a MS DB.
i have a function that is being called from a stored proc that i want to replace and i think it will be fine but im not sure in all cases.
this is the current function that is being called.
ALTER function [dbo].[GREATEST_DATETIME]
(
@i1_p datetime,
@i2_p datetime
)
returns datetime as
begin
declare @r_l datetime
if @i1_p is null or @i2_p is null
return null
set @r_l = @i1_p
if @i2_p > @r_l
set @r_l = @i2_p
return @r_l
end
I want to replace the call to the function entirely with this.
select CASE WHEN @date1 >= @date2 THEN @date1 ELSE @date2 END
They both determine the largest date out of 2 dates im just not sure if im covering all my bases.
After testing
declare @date1 datetime
declare @date2 datetime
set @date1 = '2015-05-01'
set @date2 = null
select CASE WHEN @date1 >= @date2 THEN @date1 ELSE @date2 END
This returns null which is the desired result
Here is my final result that is going to work
declare @date1 datetime
declare @date2 datetime
set @date1 = null
set @date2 = '2015-05-01'
select CASE WHEN @date1 is null or @date2 is null then null WHEN @date1 >= @date2 THEN @date1 ELSE @date2 END