Home > Archive > Microsoft SQL Server forum > April 2005 > Get lowest date









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Get lowest date
Neil

2005-04-22, 7:23 am

Is there a more elegant way to do the following?...

declare @d1 datetime, @d2 datetime
set @d1 = '2005-01-01'
set @d2 = '2005-02-01'

select
case
when datediff(dd,@d2,@d1)
is null
then coalesce(@d1, @d2)
when (datediff(dd,@d2,@d1
)> 0)
then @d2
else @d1
end


Cheers,..
N
David Portas

2005-04-22, 7:23 am

Try:

SELECT
CASE WHEN @d1<@d2
THEN @d1
ELSE COALESCE(@d2,@d1)
END

or:

SELECT MIN(dt)
FROM
(SELECT @d1 UNION ALL
SELECT @d2)T(dt)

--
David Portas
SQL Server MVP
--

NAJH

2005-04-22, 7:23 am

That's great! Much cleaner. Thank you.

Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2008 droptable.com