ehsan0x
ehsan0x

Reputation: 670

Update query does not recognize the fields of the table it is run on

I have a table named history with fields:

id, accessionNumber, date_borrowed, date_returned, fine

I intend to update the table's row in the following manner when the query is run:

1- insert today's date into return_date

2- subtract the borrow_date from return_date(=today's date), multiply by a constant that I am going to send as a parameter and insert the result into fine

I have been looking around for hours now, but as I'm a beginner I couldn't get much out of it.

This is my query that doesn't work, what am I doing wrong?

UPDATE history
SET date_returned = CURDATE() ,
fine=DATEDIFF(CURDATE(),STR_TO_DATE(history.date_borrowed, '%m/%d/%Y')) * @some_constant
WHERE id = @id and accessionNumber=@accessionNumber

I am using MySQL Workbench 6 and C# if that's is going to help.

The error message I am getting is :

Incorrect datetime value: '2013-12-11' for function str_to_date

Thanks in advance for any help.

Upvotes: 0

Views: 52

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270643

I am going to guess that borrow_date is really stored as a date and not as a string. If so, then the query can be simplified to:

UPDATE history
    SET date_returned = CURDATE() ,
        fine = DATEDIFF(CURDATE(), borrow_date) * some_constant
    WHERE id = @id and accessionNumber=@accessionNumber;

Upvotes: 1

Related Questions