Q:

Update a table column with Value based on the same table fied

I have the below table Structure.
Order_Table
Ord_ID  Ord_Date          Del_Date         Del_Qty  Del_Time  Hits  Miss

10001   9/13/2013 12:30   9/13/2013 13:10  2        40        2     0

10002   9/13/2013 13:09   9/13/2013 15:10  1        119       0     1

10003   9/13/2013 16:20   9/13/2013 16:50  1        30        1     0

I want to update the table Del_Time based on (Ord_Date - Del_Date), Hits should populate with value of Del_Qty field if the Del_Time is less than 60 and Miss should be populated with value from Del_Qty if Del_Time is greater than 60.
Is this Possible? Am using MS Sql Server

A:

The difference between two datetime values is a datetime.  It doesn't make sense to say "less than 60".  I think you want minutes (this is a guess).  If so, you want to use the datediff() function.
Otherwise, this is fairly simple update syntax, with a case.  The case is used to avoid updating values when the conditions are not met:
update order_table
    set del_time =  datediff(minute, del_date, ord_date),
        hits = (case when datediff(minute, del_date, ord_date) < 60
                     then del_qty else hits
                end),
        miss = (case when datediff(minute, del_date, ord_date) > 60
                     then del_qty else miss
                end)

