php - How to Delete MSSQL Table Rows if they are Older than One Year/One Day -
i trying create php query delete sql table rows if order_date
column on year old. second query table rows deleted if same column day old.
to go more in depth, order_date
column in current table, tracking_orders
, , created using part of query: order_date datetime not null default getdate(),
. tracking_orders
table looks this:
order_date tracking_order account_id apr 7 2014 3:49pm 1 1 apr 7 2014 3:51pm 2 1
the day asking question april 8th 2014, trying make query delete rows if order_date
data apr 7 2013, , query delete rows if order_date
data apr 7 2014. also, if data more 1 year old, still delete. example, apr 7 2012.
for code example, looking this:
$sql = 'delete * tracking_orders order_date == date_sub(getdate(), interval 1 year)';
and this, delete after 1 day:
$sql = 'delete * tracking_orders order_date == date_sub(getdate(), interval 1 day)';
note: above doesn't work because date_sub
mysql syntax, , working mssql.
thank help. appreciated.
try this:
delete tracking_orders datediff(dy,order_date, getdate()) = 1 or datediff(yy,order_date, getdate()) > 1
the first condition in checks difference of 1 day, , second difference greater 1 year. note going strictly calendar below return 1 though time difference less 24 hours:
select datediff (dy, '2013-01-01 23:00:00.000', '2013-01-02 22:00:00.000')
to check 24 hour difference use datediff(hh,order_date, getdate()) = 1
Comments
Post a Comment