| 
                
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. 
    
        | 
                
                    | 
                            
                                | Author | Topic |  
                                    | jgzabala@yahoo.comStarting Member
 
 
                                        18 Posts | 
                                            
                                            |  Posted - 2009-09-14 : 22:02:31 
 |  
                                            | Hi,Can you help me how to compute difference betweendates. result should be yy/mm/dd. already usedfnTimeApart but i need the total year, month and day betweenthe records below. sampleType	Date From	date To	       Year    Month	DayLPG	12/09/1995	16/03/1996	0	6	4LPG	17/03/1996	20/07/1996	0	4	3LPG	17/08/1996	16/05/1997	0	8	29LPG	21/10/1997	27/05/1998	0	7	6LPG	01/05/1999	13/04/2000	0	11	12LPG	20/08/2002	01/11/2002	0	2	12LPG	30/04/2003	21/07/2003	0	2	21LPG	22/07/2003	22/06/2003	0	1	0LPG	14/10/2004	27/12/2004	0	2	13LPG	18/03/2005	11/11/2005	0	7	24LPG	17/03/2006	31/08/2006	0	5	14LPG	05/09/2006	01/01/2007	0	3	27LPG	01/01/2007	25/01/2007	0	0	24LPG	15/06/2007	28/03/2008	0	9	13LPG	06/11/2008	03/12/2008	0	0	27thanks :-)edit: moved to proper forum |  |  
                                    | visakh16Very Important crosS Applying yaK Herder
 
 
                                    52326 Posts | 
                                        
                                          |  Posted - 2009-09-16 : 13:01:56 
 |  
                                          | [code]SELECT Type,DateFrom,DateTo,CASE WHEN MONTH(DateTo)<MONTH(DateFrom) OR (MONTH(DateTo)=MONTH(DateFrom) AND DATEPART(dd,DateTo)<DATEPART(dd,DateFrom)) THEN DATEDIFF(yy,DateFrom,DateTo)-1 ELSE DATEDIFF(yy,DateFrom,DateTo) END AS Year,CASE WHEN DATEPART(dd,DateTo)<DATEPART(dd,DateFrom)) THEN DATEDIFF(mm,DateFrom,DateTo)-1 ELSE DATEDIFF(mm,DateFrom,DateTo) END AS Month,CASE WHEN DATEPART(hh,DateTo)<DATEPART(hh,DateFrom)) THEN DATEDIFF(dd,DateFrom,DateTo)-1 ELSE DATEDIFF(dd,DateFrom,DateTo) END  AS DayFROM YourTable[/code] |  
                                          |  |  |  
                                    | jgzabala@yahoo.comStarting Member
 
 
                                    18 Posts | 
                                        
                                          |  Posted - 2009-09-17 : 22:07:27 
 |  
                                          | Hi,thanks for the answer, actually i needto compute all those dates to generate onlyone year/ month / day, summary of all types.thanks:-) |  
                                          |  |  |  
                                    | khtanIn (Som, Ni, Yak)
 
 
                                    17689 Posts | 
                                        
                                          |  Posted - 2009-09-17 : 22:14:40 
 |  
                                          | [code]select Type, sum(Year), sum(Month), sum(Day)from(    < your existing query here>) agroup by Type[/code]this will give you sum() by type. remove Type if you want a overall sum() KH[spoiler]Time is always against us[/spoiler]
 |  
                                          |  |  |  
                                    | jgzabala@yahoo.comStarting Member
 
 
                                    18 Posts | 
                                        
                                          |  Posted - 2009-09-17 : 23:08:43 
 |  
                                          | hi,thanks for the repy, if i used thesum it will just sum the year, month and days,but i need result for example.Type  Year Month DayLPG   3    12    360 which is based on the dates givenif i used sum the result will beType Year Month DayLPG  -1   65    2260thanks,:-) |  
                                          |  |  |  
                                    | khtanIn (Som, Ni, Yak)
 
 
                                    17689 Posts | 
                                        
                                          |  Posted - 2009-09-18 : 00:44:52 
 |  
                                          | how do you want to convert 2260 days to month ? assumed 30 days per month ?take this line for example : 17/03/1996 20/07/1996 0 4 3based on above, the different between the 2 date is 0 year 4 month 3 day. The month is it a 30 or 31 day month ?and just assumed these 2 linesLPG 17/03/1996 20/07/1996 0 4 3LPG 17/08/1996 16/05/1997 0 8 29total up the Year, Month, Day will give you 0 year 12 month 32 days. So how to convert 32 days to month ? can you assume it is a 30 day or 31 day ?basically summing up the year, month, days does not really make sense or rather it does not give you a meaningful result. Unless you just want an estimate or approximate result than you can assume a 30 days month just take 2260 % 30 will give you the days2260 / 30 will give you the month2260 / (30 * 12) will give you the year KH[spoiler]Time is always against us[/spoiler]
 |  
                                          |  |  |  
                                |  |  |  |  |  |