Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 retrive data before one week or month

Author  Topic 

new_developer
Starting Member

18 Posts

Posted - 2010-12-05 : 13:59:24
hi for all i need sql statements to retrive the records which add

before one week ,two week,one month and two month depend on

the date column in databaase

i want to retrive the records which add before week and so on

any one can help me how i doing this

thanks

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2010-12-05 : 14:07:11
select *
from tbl
where addeddate < datead(wk,-1,getdate())

select *
from tbl
where addeddate < datead(wk,-2,getdate())

select *
from tbl
where addeddate < datead(mm,-1,getdate())

But I suspect that's not what you really want - you need to be a bit clearer.


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

new_developer
Starting Member

18 Posts

Posted - 2010-12-09 : 16:15:42
friend i try with your statment
but it retrive all the records from database
i don't want don't that
i want retrive the records added from
one week only no more
can you tell me how i do this
thanks
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2010-12-09 : 16:48:59
First you need a column that records the date that the record was added to the database. Do you have one?

--
Gail Shaw
SQL Server MVP
Go to Top of Page

new_developer
Starting Member

18 Posts

Posted - 2010-12-10 : 06:12:03
i have column in database for date
call it date
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-12-10 : 06:20:48
How about changing the symbol used in nigel's solution :
select *
from tbl
where addeddate >= dateadd(wk,-1,getdate())

select *
from tbl
where addeddate >= dateadd(wk,-2,getdate())

select *
from tbl
where addeddate >= dateadd(mm,-1,getdate())


If this is not your requirement then please pass on some sample data along with expected output.
Go to Top of Page
   

- Advertisement -