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
 General SQL Server Forums
 New to SQL Server Programming
 Date Function

Author  Topic 

satheesh
Posting Yak Master

152 Posts

Posted - 2012-09-11 : 09:42:58
Dear All,

I try to retrieve data based on date condition (last year currentdate + 24 day).when i use date function i am getting wrong result.

startdate = DATEADD(year,-1,dateadd(day,24,dateadd(day,datediff(day,0,getdate()),0))) - result 86rows

if i use this -> (policy.StartDate between '2011-10-05 00:00:00.000' AND '2011-10-05 23:59:59.997') - result 137 rows

what i am doing wrong in date function and how to change this.(i am getting different result when using function)

Any help will be highly appreciated!

Thanks

Regards,
SG

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2012-09-11 : 09:56:54
Your first query returns values where stardate = '2011-10-05 00:00:00.000',exactly. The second returns anything that happened on 10/5

startdate >=DATEADD(DAY,datediff(day,'19010101',CURRENT_TIMESTAMP),23)
and startdate <DATEADD(DAY,datediff(day,'19010101',CURRENT_TIMESTAMP),24)

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

satheesh
Posting Yak Master

152 Posts

Posted - 2012-09-11 : 10:37:45
Thanks Jimf.Its working perfectly!!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-11 : 10:57:40
understand the logic behind date based filtering here

http://visakhm.blogspot.com/2012/07/generate-datetime-values-from-integers.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -