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 selection

Author  Topic 

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2009-07-17 : 16:41:21
I have a query that selects by date. The result is that upper date being displayed is 1 day less than what I requested (in my example 2009-07-13 is the highest date even if I set @DateTo = 2009-07-14. If I now set @DateTo = 2009-07-15, the upper date being displayed is 2009-07-14 etc.


set @Product = 'ULS'
set @DateFrom = '2009-07-06'
set @DateTo = '2009-07-14'


select [load],
date,
branded,
name,
cast(gross as decimal(15,2)),
cast(net as decimal(15,2))
from [SFM-TP6000-1].TP6000.dbo.loadcomp as load1
inner join [SFM-TP6000-1].TP6000.dbo.product as prod1 on prod1.product = load1.branded
where (@Product is null or branded = @Product)
and (@DateFrom IS Null or date >= @DateFrom)
and (@DateTo IS Null or date <= @DateTo)
order by date

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-07-17 : 16:43:50
[code] and (@DateTo IS Null or date <= DATEADD(DAY, 1, @DateTo))[/code]



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2009-07-17 : 16:47:34
I understand that you are adding 1 day to the @DateTo. Why is that I have to do that?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-07-17 : 16:50:13
I believe the Date column contains time information and as such

2009-05-14 16:00 (Date column) is greater than 2009-05-14 (@DateTo variable)



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2009-07-17 : 16:53:54
Did not think of that, but you are correct. Good catch.

Ha en trevlig helg.....
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-07-17 : 16:57:27
Du med!

You too...



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -