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

What is mean by dml?

0 Answers  


What are the differences between sql server and mysql.

0 Answers  


What are Spatial data types in SQL Server 2008

0 Answers   Infosys,


New concepts of sql server 2005 use in your project.

0 Answers   TCS,


What are the different types of locks in the database?

0 Answers  






Please explain the characteristics of a transaction server for example atomicity, consistency, isolation, durability?

0 Answers  


How many types of dimensions are there and what are they? : sql server analysis services, ssas

0 Answers  


What is Stored Procedure? What is Views in sql server? Difference between a User Defined Function and a Stored Procedure Difference between a primary key and a unique key? What is a join and explain different types of joins. Difference between temp table and table variable Difference between Triggers and Stored Procedures Difference between UNION ALL Statement and UNION What is COALESCE / Why do we use COALESCE? Why we use SET ROWCOUNT in Sql How many clustered index can have a table How many types of local tables in SQL SERVER Difference between DELETE and TRUNCATE What is Aggregate Functions? What is Row_Number()? What are Ranking Functions? What is NOLOCK? What is CTE? What are the Advantages of using CTE? What is the STUFF function and how does it differ from the REPLACE function? What are the difference between clustered and a non-clustered index? What are the different index configurations a table can have? Difference between a HAVING CLAUSE and a WHERE CLAUSE? Difference between SET and SELECT Provide all the built in string function of SQL SERVER Difference between char and varchar data types Define candidate key, alternate key, composite key. What are constraints? Explain different types of constraints. What is a self join? Explain it with an example. How will you convert table row to a column comma separated value

4 Answers   ACS,


two tables with table name ship(name,year) and battle (name,year),how to find the latest added year in the table ship

1 Answers  


Diffrences between sql server 2000 vs 2008

0 Answers   TCS,


Diff between Composite key, Alternate Key, Candidate Key, Primary Key, Unique Key, Super Key, Foreign Key

1 Answers  


Explain the phases a transaction has to undergo?

0 Answers  


Categories