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 2008 Forums
 Transact-SQL (2008)
 get data based on date last two days using range

Author  Topic 

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2013-10-23 : 13:23:45
I want to get the data based on passing dates excluding hrs/mins/secs using >= and <= clause

createddatetime is a datetime column and want to get last two days (sysdate-2) based rows.

select * from T_Prog_ACCTs where createddatetime

Thank you very much for the helpful info.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-23 : 13:47:44
[code]
select * from T_Prog_ACCTs where createddatetime>=DATEADD(dd,DATEDIFF(dd,0,GETDATE()),-2)
AND createddatetime < DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0)
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-23 : 13:48:45
also see

http://visakhm.blogspot.in/2012/12/different-ways-to-implement-date-range.html
http://visakhm.blogspot.in/2012/07/generate-datetime-values-from-integers.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2013-10-23 : 14:39:08
As always , Thank you very much Visakh Sir for all the help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-24 : 01:31:49
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

djj55
Constraint Violating Yak Guru

352 Posts

Posted - 2013-10-24 : 07:18:14
visakh16, what is the difference between DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0) and CAST(GETDATE() AS DATE)?

djj
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-24 : 08:53:31
quote:
Originally posted by djj55

visakh16, what is the difference between DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0) and CAST(GETDATE() AS DATE)?

djj


Both are equivalent
one does a type conversion to date datatype whilst other does time part stripping using date functions. date datatype is available only from SQL 2008 onwards so CAST method works only in those versions. Other method works in all versions. I prefer date function method.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

djj55
Constraint Violating Yak Guru

352 Posts

Posted - 2013-10-24 : 09:02:30
visakh16, Thanks for the information.

djj
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-24 : 12:10:07
you're welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -