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 |
|
Nikkap
Starting Member
8 Posts |
Posted - 2007-03-12 : 09:17:45
|
| I have a table "Periods" StartPeriod EndPeriod ----------- ----------10/03/2003 30/11/200401/12/2004 15/12/200416/12/2004 31/12/2006desired output StartPeriod LastPeriod DatesInLeapYear----------- --------- ------------10/03/2003 30/11/2004 33401/12/2004 15/12/2004 1316/12/2004 31/12/2005 15 I would like the day difference only for those days which are in a leap year Thank u very much |
|
|
mk_garg20
Constraint Violating Yak Guru
343 Posts |
Posted - 2007-03-12 : 23:34:56
|
| is it some kind of school exercise?Post your table structure.I think you need a stored procedure to do this.mk_garg |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-13 : 00:14:40
|
[code]-- Create the functioncreate function fn_DaysInLeapyear( @start_date datetime, @end_date datetime) returns intasbegin declare @yr int, @days int select @yr = year(@start_date), @days = 0 while @yr <= year(@end_date) begin if (@yr % 100 = 0 and @yr % 400 = 0) -- Leap Year or (@yr % 100 <> 0 and @yr % 4 = 0) begin select @days = @days + case when @yr = year(@start_date) and @yr = year(@end_date) then datediff(day, @start_date, @end_date) when @yr = year(@start_date) then datediff(day, @start_date, dateadd(year, datediff(year, 0, @start_date) + 1, -1)) when @yr = year(@end_date) then datediff(day, dateadd(year, datediff(year, 0, @end_date), 0), @end_date) else datepart(dayofyear, convert(varchar(4), @yr) + '1231') end end select @yr = @yr + 1 end return @daysendgo-- Create the table variabledeclare @Periods table( StartPeriod datetime, EndPeriod datetime)-- insert sample datainsert into @Periodsselect '20030310', '20041130' union allselect '20041201', '20041215' union allselect '20041216', '20061231'-- queryselect *, DatesInLeapYear = dbo.fn_DaysInLeapyear(StartPeriod, EndPeriod)from @Periods[/code] KH |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-13 : 01:49:07
|
| khtan, the year of 2100 is NOT a leap year, even if modula 4 is zero...IF Year % 400 = 0 THEN LeapYear = TrueELSEIF Year % 100 = 0 THEN LeapYear = FalseELSEIF Year % 4 = 0 THEN LeapYear = TrueEND IFPeter LarssonHelsingborg, Sweden |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-13 : 02:15:02
|
quote: Originally posted by Peso khtan, the year of 2100 is NOT a leap year, even if modula 4 is zero...IF Year % 400 = 0 THEN LeapYear = TrueELSEIF Year % 100 = 0 THEN LeapYear = FalseELSEIF Year % 4 = 0 THEN LeapYear = TrueEND IFPeter LarssonHelsingborg, Sweden
Oh yeah. forgotten about that. . 2000 seems so far away now KH |
 |
|
|
Nikkap
Starting Member
8 Posts |
Posted - 2007-03-13 : 03:42:07
|
| Thank u so much khtan This is all I wanted .Thank u again !!!!! |
 |
|
|
|
|
|
|
|