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 2005 Forums
 Transact-SQL (2005)
 date

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2007-12-06 : 04:39:44
Hi,
One of the fields in a table is called Import_Date and it is of type smalldatetime. So it shows dates such as:
2007-11-16 11:31:00
2007-11-16 11:31:00
2007-11-16 11:31:00
2007-11-16 11:31:00
...
...

Now I would like to have a select query which shows the dates between last week and today
something like
select Import_Date from table1
where
Import_Date between dateadd(wk, -1, Import_Date) and getdate()

Thanks

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-12-06 : 04:41:09
select Import_Date from table1
where Import_Date >=dateadd(day, -7, and getdate())



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2007-12-06 : 04:45:10
If now is say 10:00 am then doe this query return everything from before 10:00 am since 7 days ago?
It seems not.
I also want to get everything from before the present time on the start date
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-12-06 : 04:51:48
select Import_Date from table1
where Import_Date >=dateadd(day, -7, and dateadd(day,datediff(day,0,getdate()),0)


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2007-12-06 : 04:56:59
great, thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2007-12-06 : 05:00:22
Strip off the time part from start date using any of methods so that it takes records from last week regardless of time.

something like

Import_Date between dateadd(wk, -1,CAST(CONVERT(varchar(11), getdate())AS datetime)) and getdate()
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-12-06 : 05:01:03
Well. That should be

select Import_Date from table1
where Import_Date >=dateadd(day, -7, dateadd(day,datediff(day,0,getdate()),0))


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-12-06 : 05:02:26
quote:
Originally posted by visakh16

Strip off the time part from start date using any of methods so that it takes records from last week regardless of time.

something like

Import_Date between dateadd(wk, -1,CAST(CONVERT(varchar(11), getdate())AS datetime)) and getdate()



You should avoid converting dates to varchar.
See my previous reply


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -