Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

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

 All Forums
 General SQL Server Forums
 New to SQL Server Administration
 convert varchar to int
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Constraint Violating Yak Guru

332 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

Almighty SQL Goddess

38200 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
Go to Top of Page

Premature Yak Congratulator

22864 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())


Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next 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.03 seconds. Powered By: Snitz Forums 2000