views:

21

answers:

2

I writing code to determine how many days in a year. I am trying to keep it really simple. I found code that I think is very clean to determine a leap year. I am passing the inputted date using DATEPART(Y,@Year) to the leap year program and some how am not getting the correct results so I has to be in my SQL code to process the input date as the correct bit is returned.

Here is the code for the Leap Year:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER FUNCTION [dbo].[FN_Is_Leap_Year]
(
 -- the parameters for the function here

        @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

Here is the code I wrote to process the input date & get the # days in a year:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER FUNCTION [dbo].[FN_Get_Days_In_Year]
(
    @InputDT    varchar(10)
)

RETURNS int
AS
BEGIN

DECLARE  @Result  int,
         @Year    int   


Set @Result = 
 CASE
    WHEN dbo.FN_Is_Leap_Year(Datepart(yyyy,@Year)) = 0 Then 365
    WHEN dbo.FN_Is_Leap_Year(Datepart(yyyy,@Year)) = 1 Then 366
 END


        RETURN @Result
    END
A: 

In the code you've posted for FN_Get_Days_In_Year you don't seem to be assigning a value to @Year anywhere.

Martin Smith
Yes, I just caught that too. here is code now (tho it still ignores the return of the 1 bit. Maybe I did this set wrong?SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER FUNCTION [dbo].[FN_Get_Days_In_Year]( @InputDT varchar(10))RETURNS VARCHAR(3)ASBEGINDeclare @Year int, @Result int Set @Year = datepart(y,@InputDT) Set @Result = CASE WHEN dbo.FN_Is_Leap_Year(Datepart(yyyy,@Year)) = 0 THEN 365 else 366 END Return @ResultEND
JMS49
caught the missing 'y' in Set @Year=Datepart(yy,@inputDate)
JMS49
A: 

Got it working!!

GO ALTER FUNCTION [dbo].[FN_Get_Days_In_Year] ( @InputDT int )

RETURNS varchar(3) AS BEGIN

Declare @Year int,
@RetVal bit, @Result varchar(3)

Set @Year = datepart(yy, @InputDT) Set @RetVal = dbo.FN_Is_Leap_Year(Datepart(yy,'2012'))
Set @Result= Case When @RetVal = 1 then 366 else 365 End

Return @Result

END

JMS49
Good. I don't understand what's going on with your types though. It should return an int not varchar(3) and accept a date datatype. Also unless you are using this in a lot of places I'd probably dispense with the UDF for this and use something like `CASE WHEN Col1 % 4 > 0 THEN 365 WHEN Col1 % 400 = 0 THEN 366 WHEN Col1 % 100 = 0 THEN 365 ELSE 366 END`
Martin Smith
Nope I am wrong, i Validated the bit is passing okay, but the code is still not returning correctly, any help is appreciated.GOALTER FUNCTION [dbo].[FN_Get_Days_In_Year]( @InputDT int ) RETURNS varchar(3)ASBEGINDeclare @Year int, @RetVal bit, @Result varchar(3) Set @Year = datepart(yy, @InputDT)Set @RetVal = dbo.FN_Is_Leap_Year(Datepart(yy,@Year)) Set @Result = Case When @RetVal = 1 then 365 Else 366 end Return @ResultEND
JMS49
Martin, I am sorry I am not totally follow your Case statement can you elaborate?
JMS49
Martin, I have working through you code:CASE WHEN Col1 % 4 > 0 THEN 365 WHEN Col1 % 400 = 0 THEN 366 WHEN Col1 % 100 = 0 THEN 365 ELSE 366 ENDgot it working!
JMS49