SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQLHelp With Datediff
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

otakubakaa
Starting Member

6 Posts

Posted - 07/25/2012 :  01:47:54  Show Profile  Reply with Quote
mods: please close thread

Edited by - otakubakaa on 07/25/2012 12:53:53

webfred
Flowing Fount of Yak Knowledge

Germany
8513 Posts

Posted - 07/25/2012 :  02:49:24  Show Profile  Visit webfred's Homepage  Reply with Quote
Can you please tell us the data type of Search_Date?
And if it is not DATE or DATETIME then please post an example on how the values are formatted in that column.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

otakubakaa
Starting Member

6 Posts

Posted - 07/25/2012 :  09:35:50  Show Profile  Reply with Quote
quote:
Originally posted by webfred

Can you please tell us the data type of Search_Date?
And if it is not DATE or DATETIME then please post an example on how the values are formatted in that column.


No, you're never too old to Yak'n'Roll if you're too young to die.



sorry, data type is datetime

Edited by - otakubakaa on 07/25/2012 10:30:19
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29138 Posts

Posted - 07/25/2012 :  09:38:31  Show Profile  Visit SwePeso's Homepage  Reply with Quote
DELETE FROM dbo.Table_Name WHERE Search_Date < DATEADD(MONTH, -2, GETDATE())



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

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 07/25/2012 :  10:17:26  Show Profile  Reply with Quote
DELETE FROM dbo.Table_Name WHERE Search_Date < DATEADD(MONTH,DATEDIFF(MONTH, 0, GETDATE())-2,0)

if you want to consider start of day (0000 hrs) as boundary value

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

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 07/25/2012 :  12:18:04  Show Profile  Reply with Quote
nope this will search from first date of the previous 2 month period


DELETE FROM dbo.Table_Name WHERE Search_Date < DATEADD(MONTH,DATEDIFF(MONTH, 0, GETDATE())-2,0)

see this simple example

DECLARE @dateval datetime

SET @dateval='20120815'

SELECT DATEADD(MONTH,DATEDIFF(MONTH, 0, @dateval)-2,0)





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

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 07/25/2012 :  12:54:35  Show Profile  Reply with Quote
the datediff part returns number of months elapsed from basedate (1 jan 1900) till current datemonths. then it subtracts 2 from it to reach two months prior to current month. adding this month value again to basedate using dateadd causes it to return 1st of that month

see this for similar logics

http://visakhm.blogspot.com/2010/01/some-quick-tips-for-date-formating.html

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000