tags:

views:

51

answers:

3
+2  Q: 

Date issue in sql

I have a table that I am entering data into every day. But I don't want that this data to be modified by other users. So I am backing up this data to another table using:

INSERT INTO  tbl_cancel_backup 
  SELECT tbl_cancel.[cdate] AS 'cdate',
         tbl_cancel.[machine_no] As 'No', 
         Sum(tbl_cancel.[amount]) AS 'Total' 
    FROM tbl_cancel 
   WHERE tbl_cancel.[Cdate]=@canceldate 
GROUP BY tbl_cancel.[Machine_no], tbl_cancel.[cdate];

After this operation, I would like to delete records which are 2 days earlier. How I have to modify below code to proper working one?

DELETE FROM tbl_cancel WHERE cdate = CONVERT (date, GETDATE()-2);

Thanks to everyone.

+1  A: 

You want to use DateAdd T-SQL

http://msdn.microsoft.com/en-us/library/ms186819.aspx

DELETE FROM tbl_cancel WHERE cdate <= DATEADD(d, -2, GETDATE())

Nate Zaugg
answered with only the link after I'd already posted the sql, then pasted the exact same sql in after that...and getting more votes...well played
kekekela
Thaks for helping me. Working like a char...
Hakan
@kekekela - Actually I posted first but it changed my posted date when I updated it. I didn't steal your answer!
Nate Zaugg
"Actually I posted first " -- No, you didn't, you pasted the SQL in after I'd already responded with it.
kekekela
Yeah, because my SQL is the exactly the same as your SQL and I've never done a DATEADD in 10 years of consulting! That is what happened, yeah! I didn't copy your stupid SQL! Get a life dude!
Nate Zaugg
+1  A: 

delete from tbl_cancel where cdate <= dateadd(d,-2,getdate())

kekekela
A: 

You could try using the DATEADD syntax:

DELETE FROM tbl_cancel WHERE cdate <= CONVERT (date, DATEADD(dd,-2,GETDATE());
Matthew Jones