sql server - Equivalent of GETDATE() BETWEEN Two DateTime Fields where Both can be NULL -
is
select [id] ,[dateonline] --nullable ,[dateoffline] --nullable ,[pageid] ,[downloadid] ,[weight] [downloadpage] getdate() between [dateonline] , [dateoffline] equivalent to:
select [id] ,[dateonline] --nullable ,[dateoffline] --nullable ,[pageid] ,[downloadid] ,[weight] [downloadpage] ([dateonline] null or [dateonline] <= getdate()) , ([dateoffline] null or [dateoffline] > getdate()) but catering nulls?
or there more elegant way of doing it?
where parentheses needed here?
thanks.
edit:
both [dateonline] , [dateoffline] of type datetime
if [dateonline] null logic "online now"
if [dateoffline] null logic "never go offline (once online)"
sorry, should have included in question begin with.
the author's second query net better performance if there no indexes on columns. if there indexes, that's no brainer... using coalesce disable index , table scan instead of index seek (very bad performance).
even if there aren't indexes on columns, "is null" return constant... whereas, in case of coalesce function, null values still need evaluated each time. if have table full of null dateonline , dateoffline, performance leak cannot ignored.
in case, can't think of reason why you'd use coalesce in case.
also, i'm guessing (since you're checking date range) 2 dates or nothing. have check 1 of dates.
([dateonline] null) or getdate() between [dateonline] , [dateoffline]
Comments
Post a Comment