SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Query to get max date from different fields
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

PeeJay2583
Starting Member

India
39 Posts

Posted - 07/17/2012 :  03:28:49  Show Profile  Reply with Quote
Hi,

I have a table where there are 6 fields.

ID fld1 fld2 fld3 fld4 fld5
1 04/10/10 05/12/12 NULL NULL NULL
2 03/1/09 NULL NULL 12/01/12 NULL

The result I am looking for is max date from all five fields
ID Max Date
1 05/12/12
2 12/01/12

Any help will be appreciated..Thanks

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 07/17/2012 :  04:21:47  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
;with cte as
(
select id, fld1 from #MaxDate
union all
select id, fld2 from #MaxDate
union all
select id, fld3 from #MaxDate
union all
select id, fld4 from #MaxDate
union all
select id, fld5 from #MaxDate
)
select id, max(fld1) from cte group by id


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

PeeJay2583
Starting Member

India
39 Posts

Posted - 07/17/2012 :  05:00:53  Show Profile  Reply with Quote
This query is giving the date of fld 2 where fld2 date is greater than fld 2 and not checking dates with fld 3,fld 4 and fld 5 where in fld 5 date is the max
Go to Top of Page

PeeJay2583
Starting Member

India
39 Posts

Posted - 07/17/2012 :  05:11:33  Show Profile  Reply with Quote
Nigel,

Your query is giving the result i was looking for.


Thank you both Lion and Nigel.

Great help!
Go to Top of Page

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 07/17/2012 :  05:35:04  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
lol :).

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000