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.
| 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:002007-11-16 11:31:002007-11-16 11:31:002007-11-16 11:31:00......Now I would like to have a select query which shows the dates between last week and todaysomething likeselect Import_Date from table1whereImport_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 table1where Import_Date >=dateadd(day, -7, and getdate())MadhivananFailing to plan is Planning to fail |
 |
|
|
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 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-06 : 04:51:48
|
| select Import_Date from table1where Import_Date >=dateadd(day, -7, and dateadd(day,datediff(day,0,getdate()),0)MadhivananFailing to plan is Planning to fail |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2007-12-06 : 04:56:59
|
| great, thanks |
 |
|
|
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() |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-06 : 05:01:03
|
| Well. That should beselect Import_Date from table1where Import_Date >=dateadd(day, -7, dateadd(day,datediff(day,0,getdate()),0))MadhivananFailing to plan is Planning to fail |
 |
|
|
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 MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|