October 19, 2008 at 11:39 pm
I have this view viewALL that gets information based on 4 weeks from the days date.
SELECT TOP (100) PERCENT AllocatedHrs AS TotalHours, AllocatedHrs - AllocatedHrs AS PastDemand, DATEPART(wk, ActualDueDate) AS TheWeekID,
MachineID
FROM dbo.tblMachineRunTime
WHERE (TaskStatusID = 2) AND (ActualDueDate BETWEEN GETDATE() - 42 AND GETDATE() - 14)
ORDER BY TheWeekID
How can i format t he code to detect leap year.
Thanks very much
October 20, 2008 at 1:09 am
Not sure what you bean by detect the leap year - your code seems to be using a fixed 28 day window rather than anything to do with months (specifically february) so I am not sure what leap years have to do with it.
Anyway the formula for a leap year can be built as a case expression:
select case when ( year(getdate()) % 4 = 0 AND YEAR( getdate()) % 100 != 0) or year(getdate()) % 400 = 0 hen 1 else 0 end as leap
which may help.
I woul d also start avoiding the trick of select top 100% in a view, it may appear to work but it is well documented that this should really be regarded as a loophole at 200 and best avoided.
Mike John
October 20, 2008 at 7:20 am
Found this trick for determing a leap year (thought it came from this site)
if object_id('udf_IsLeapYear') is not null
drop function udf_IsLeapYear
GO
create function dbo.udf_IsLeapYear (@year int)
returns bit
as
begin
return(select case datepart(mm, dateadd(dd, 1, cast((cast(@year as varchar(4)) + '0228') as datetime)))
when 2 then 1
else 0
end)
end
go
Wilfred
The best things in life are the simple things
October 20, 2008 at 8:04 am
Nice, I like that one.
Is that the original name?
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
October 20, 2008 at 8:23 am
No. Found the original location: http://www.mssqltips.com/tip.asp?tip=1527
Wilfred
The best things in life are the simple things
October 20, 2008 at 9:14 am
Thanks!
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
October 25, 2008 at 7:14 pm
Heh... you can do it a bit easier than that... not sure it's even worth putting in a function...
[font="Courier New"]DECLARE @Year INT
SET @Year = 2007
SELECT ISDATE(STR(@Year)+'0229')[/font]
--Jeff Moden
Change is inevitable... Change for the better is not.
October 25, 2008 at 7:51 pm
Nice!
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
October 27, 2008 at 1:05 am
If you had a Date you could also use
Declare @Date int
Set @Date = getDate()
Select Case Datepart( year, @Date ) % 4
when 0 Then 1
Else 0
End
October 27, 2008 at 3:56 am
No, that's incorrect.
See the exact formulas for determing a leap year on http://en.wikipedia.org/wiki/Leap_year
Wilfred
The best things in life are the simple things
October 27, 2008 at 4:31 am
Interesting article. even there it seems one can calculate it with different functions ...
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply