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
Can two tables share a primary key?
How to list all field names in the result set using mssql_field_name()?
How do I get to sql server configuration manager?
What is the distinction amongst delete and truncate?
What do you mean by recursive stored procedure?
How and why use sql server?
What is user-defined function?
In which files does sql server actually store data?
what is raid and what are different types of raid configurations? : Sql server database administration
What is the difference between count and distinct count?
What is normalization? Explain different forms of normalization?
Is sql server implemented as a service or an application? : Sql server database administration
What are the underflow and overflow behaviors on float literals?
How can you tell if a database object is invalid?
What are the advantages of using stored procedures?