How to get number of days in a given year?

Answer Posted / anamika devara

Getting the number of days in a year is fairly easy because you are just choosing between 365 and 366, with the latter only happening every 4 years or every leap year.  To determine if it is a leap year, either of the following conditions must be met:




The year must be divisible by 4 and must NOT be divisible by 100.
The year must be divisible by 400.
Below is a user-defined function which accepts a date as a parameter and returns the number of days in that year.





CREATE FUNCTION [dbo].[ufn_GetDaysInYear] ( @pDate    DATETIME )
RETURNS INT
AS
BEGIN




    DECLARE @IsLeapYear        BIT




    SET @IsLeapYear = 0
    IF (YEAR( @pDate ) % 4 = 0 AND YEAR( @pDate ) % 100 != 0) OR
        YEAR( @pDate ) % 400 = 0
        SET @IsLeapYear = 1




    RETURN 365 + @IsLeapYear




END
GO

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Do you know sql server 2008 backup compression?

654


What does man by sql wildcard characters in sql server?

620


What are different types of subquery?

819


Explain different backup plans?

617


What is sql injection? How to protect against sql injection attack?

641






Is there any difference between the primary key and unique key?

668


Can we take the full database backup in log shipping?

623


Explain the third normal form(3nf)?

605


Do you know how to store and query spatial data?

684


Why use stored procedures in sql server?

622


What is cached report?

112


What is the server name for sql management studio?

549


How to transfer an existing table from one schema to another schema in ms sql server?

594


What is the difference between sql server 2000 object owner and sql server 2005 schema? : sql server database administration

674


Explain partitioned view?

690