469,106 Members | 2,313 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,106 developers. It's quick & easy.

Get lowest date

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
Jul 23 '05 #1
2 3990
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
--

Jul 23 '05 #2
That's great! Much cleaner. Thank you.

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by nick_faye | last post: by
17 posts views Thread by rhitz1218 | last post: by
4 posts views Thread by tomek milewski | last post: by
namcintosh
54 posts views Thread by namcintosh | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.