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 Administration
 convert varchar to int
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

laddu
Constraint Violating Yak Guru

USA
331 Posts

Posted - 08/15/2014 :  14:10:51  Show Profile  Reply with Quote
I have a table with date column and need to delete the data more than 30 days old.

I tried this script but getting error

DELETE table WHERE MsgDate < Datediff(dd, 30, Getdate())

Conversion failed when converting the varchar value '2014-08-15' to data type int.

how do i convert this varchar to int? thanks

tkizer
Almighty SQL Goddess

USA
36983 Posts

Posted - 08/15/2014 :  14:19:04  Show Profile  Visit tkizer's Homepage  Reply with Quote
You need to use DATEADD, not DATEDIFF. And use -30 instead of 30.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22756 Posts

Posted - 08/18/2014 :  03:46:27  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Also it looks like MsgDate is of varchar datatype. You need to CAST it to a DATETIME datatype.

DELETE table WHERE cast(MsgDate as datetime) < Dateadd(day, -30, Getdate())

Madhivanan

Failing to plan is Planning to fail
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.05 seconds. Powered By: Snitz Forums 2000