Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Min command

Author  Topic 

vtxvtx
Starting Member

18 Posts

Posted - 2012-09-04 : 07:21:43
Hi,

I've been trying to work out a min command (not for min in a column)
what I want to do is as follows

Datediff(mm,Date1,Min(Date2,Date3))

Is there a command out there that can do that in sql?

Thanks,
Andy

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-09-04 : 07:35:19
datediff(mm,Date1,case when Date2 < Date3 then Date2 else Date3 end)


Too old to Rock'n'Roll too young to die.
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-09-04 : 07:37:54
There are no column-wise aggregate functions (MIN, MAX, AVG etc.) in T-SQL. You can make do with the following if you are looking for min between only two columns. If you have many columns that is in your aggregate, there are other ways, but none of them very pretty.
Datediff(mm,Date1,Min(CASE WHEN Date2 < Date3 THEN Date2 ELSE Date3 END))
Go to Top of Page

vtxvtx
Starting Member

18 Posts

Posted - 2012-09-04 : 07:48:14
Thanks sunitabeck, There are 5 dates that I need to compare. I'll just write it as a case, was hoping not to do that, but oh well. Will just hope I dont need to add in any more dates to compare :).
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-09-04 : 08:24:56
If you use version 2008, you can use VALUES CLAUSE too
http://beyondrelational.com/modules/2/blogs/70/posts/10905/interesting-enhancements-to-the-values-clause-in-sql-server-2008.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -