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 |
magmo
Aged Yak Warrior
558 Posts |
Posted - 2013-01-08 : 01:29:37
|
When I filter based on dates on a datetime column I remove the time portion, but why is that important in order to get correct results? |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2013-01-08 : 02:18:06
|
Please give an example because there are many ways... Too old to Rock'n'Roll too young to die. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2013-01-08 : 02:22:26
|
quote: Originally posted by magmo When I filter based on dates on a datetime column I remove the time portion, but why is that important in order to get correct results?
In order to include all time, you need to exclude time part. For example if you want to get data of Dec 10, 2012, you have to writeWHERE date_col>='20121210' and date_col<'20121211' to include all time for Dec 10, 2012MadhivananFailing to plan is Planning to fail |
|
|
magmo
Aged Yak Warrior
558 Posts |
Posted - 2013-01-08 : 02:29:01
|
I use this approach...CREATE PROCEDURE dbo.uspTest( @FromDate DATETIME, @ToDate DATETIME)ASSET NOCOUNT ONDECLARE @Temp DATETIMEIF @FromDate > @ToDate SELECT @Temp = @FromDate, @FromDate = @ToDate, @ToDate = @TempSELECT @FromDate = DATEADD(DAY, DATEDIFF(DAY, '19000101', @FromDate), '19000101'), @ToDate = DATEADD(DAY, DATEDIFF(DAY, '18991231', @FromDate), '19000101')SELECT COUNT(ReportID) AS QtyFROM dbo.tbl_Error_ReportsWHERE DateReported >= @FromDate AND DateReported < @ToDateIt works fine but I was looking for a detailed explanation of why its so important to remove the time portion from a datetime column when searching between dates |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2013-01-08 : 02:38:11
|
quote: Originally posted by magmo I use this approach...CREATE PROCEDURE dbo.uspTest( @FromDate DATETIME, @ToDate DATETIME)ASSET NOCOUNT ONDECLARE @Temp DATETIMEIF @FromDate > @ToDate SELECT @Temp = @FromDate, @FromDate = @ToDate, @ToDate = @TempSELECT @FromDate = DATEADD(DAY, DATEDIFF(DAY, '19000101', @FromDate), '19000101'), @ToDate = DATEADD(DAY, DATEDIFF(DAY, '18991231', @FromDate), '19000101')SELECT COUNT(ReportID) AS QtyFROM dbo.tbl_Error_ReportsWHERE DateReported >= @FromDate AND DateReported < @ToDateIt works fine but I was looking for a detailed explanation of why its so important to remove the time portion from a datetime column when searching between dates
In this case the developer wanted to make sure not to use any time part even if the caller has given a time part via parameter. Too old to Rock'n'Roll too young to die. |
|
|
magmo
Aged Yak Warrior
558 Posts |
Posted - 2013-01-08 : 03:32:01
|
Yes but is that beacuse the datetime is only accurate to 3ms? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
|
|
|
|