How to get number of days in a given year?

Answers were Sorted based on User's Feedback



How to get number of days in a given year?..

Answer / akshay wadkar

DECLARE @year AS INT
SET @year=2010
Select DATEDIFF(DAY,DATEADD(YEAR,@year-1900,0)
,DATEADD(YEAR,@year-1900+1,0)) AS [TOTAL NO OF DAYS]
GO

Is This Answer Correct ?    1 Yes 0 No

How to get number of days in a given year?..

Answer / 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

How to get number of days in a given year?..

Answer / rajkumar

DECLARE @IsLeapYear        BIT




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




    select 365 + @IsLeapYear

Is This Answer Correct ?    0 Yes 0 No

How to get number of days in a given year?..

Answer / satya

create function abc(@lpyr int)
returns int
As

returns 365+@udflpyr (@lpyr)

....
User Defined function is declared to find a leap yr

Is This Answer Correct ?    0 Yes 5 No

Post New Answer

More SQL Server Interview Questions

Write a code to select distinct records without using the DISTINCT keyword.

0 Answers   Aspiring Minds,


you have developed an application which uses many stored procedures and triggers to update various tables users ocassionally get locking problems which tool is best suited to help you diagnose the problem? : Sql server administration

0 Answers  


How do you measure the performance of a stored procedure?

3 Answers   Infosys,


Which system tables contain information on privileges granted and privileges obtained

1 Answers  


Explain the difference between functions and stored procedures in sql server?

0 Answers  






Table student containing 2 columns,Join date,Employee name. Under join date 4 rows r ter =1-jan-2008,2-feb-2008,3-mar- 2008,4-feb-2008.Under Employee name 4 difeerent names jaison,robin,binoy,rahul Result set is, Table containing 4-column name=jan,feb,mar,april,,beneath these months count is given as 1,2,1,0 means these counts representing number of emplooyees joined in a month(january 1employee,february 2 employee,march 1 employee,april 0 employee) Can you give me the required sql query

3 Answers   RND Soft, Wipro,


what is the order of execution of where,having,group by in select stement

6 Answers   IBM, Tanla Solutions,


Explain trigger and its types?

0 Answers  


Differencr Between DELETE and TRUNCATE?

3 Answers   ADP,


What is normalization according to you and explain its different levels?

0 Answers  


if 3 duplicate records in a table,i want to delete 2 duplicate records by keeping 1 duplicate and 1 original as it is,how?

5 Answers  


Can you type more than one query in the query editor screen at the same time?

0 Answers  


Categories