How will u get 5 years back record?

Answers were Sorted based on User's Feedback



How will u get 5 years back record?..

Answer / chris fauvel

--crap made a booboo
--corrected code here
--Both use the dateadd function provided by t-sql, I'm sure
oracle and others have similar functions

--this way is a little slower and costlier
select *
FROM sys.tables
where modify_date between dateadd(yy, -5, getdate() ) and
getdate()

--this way is faster and less costly, by not doing the same
--function for every row.
declare @now datetime
, @then datetime
select @now = convert(datetime, convert(varchar, getdate()))
, @then = dateadd(yy, -5, @now)

Select *
from sys.tables
where modify_date between @then and @now

Is This Answer Correct ?    5 Yes 0 No

How will u get 5 years back record?..

Answer / muhammadrashidmughal

select *
from table
where date between '01/01/2003' and '12-31-2008'

or

today is 09-08-2008

select *
from table
where year(date)=year(getdate)-5 and month(date)= '08' and
day(date) ='09'

or

select *
from table
where date >='09-08-2003'
and date<='09-08-2008'

Is This Answer Correct ?    5 Yes 1 No

How will u get 5 years back record?..

Answer / chris fauvel

--Both use the dateadd function provided by t-sql, I'm sure
oracle and others have similar functions

--this way is a little slower and costlier
select *
FROM sys.tables
where modify_date between dateadd(yy, -5, getdate() ) and
dateadd(yy, -2, getdate() )

--this way is faster and less costly, by not doing the same
--function for every row.
declare @now datetime
, @then datetime
select @now = convert(datetime, convert(varchar, getdate()))
, @then = dateadd(yy, -5, @now)

Select *
from sys.tables
where modify_date between @then and @now

Is This Answer Correct ?    4 Yes 0 No

How will u get 5 years back record?..

Answer / tp

select * from table1 where date between dateadd(yy,-
5,getdate()) and getdate()

Is This Answer Correct ?    1 Yes 0 No

How will u get 5 years back record?..

Answer / krishna mohan thamisetty

select *
from table
where date between GETDATE()-(5*365) and GETDATE()

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More SQL Server Interview Questions

what is Covering Index ?

3 Answers   Thomson Reuters,


How many clustered indexes can be created on a table? I create a separate index on each column of a table. what are the advantages and disadvantages of this approach?

0 Answers  


select names starts with a,b,c

8 Answers   Scope International,


Tell me what are the essential components of sql server service broker?

0 Answers  


what is the difference detween pairwise comparison subquary and non pairwise comparison subquary?

2 Answers  






Why use update_statistics command in sql server?

0 Answers  


How to Check Whether a Global Temporary Exists in a SQL Database or not?

2 Answers  


Can a database be shrunk to 0 bytes, if not, why?

0 Answers  


What are the source of constraints?

0 Answers  


how to delete duplicate rows from table in sql server

12 Answers   HCL, UHG,


What is the system function to get the current user's user id?

2 Answers   HCL,


What are the new security features added in sql server 2012? : sql server security

0 Answers  


Categories