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
What is a Join and explain its types?
How to stop a loop early with break statements in ms sql server?
What is the optimization being performed in oracle and SQL Server?
Can we shrink data file in sql server?
What is the purpose of the tempdb database?
Why functions are used in sql server?
What are triggers? How do you invoke a trigger on demand?
What is sql stored procedure?
How to execute multiple stored procedures at one time in sql server?
What is local temp table?
What the different topologies in which replication can be configured?
What are the different types of columns types constraints in the sql server?
Name the different type of indexes in sql?
What is openrowset sql server?
how can a database be repaired? : Sql server administration