2

I've tried this

INSERT INTO products 
 (product_id, product_type, created_dt, end_dt) 
VALUES 
 ('11', '1', '2010-10-08 00:11:10', DATE_SUB(2010-10-08 00:11:10, INTERVAL 59 Minute))

But this doesn't work. Any other ways to do this within Mysql?

Thanks!

3 Answers 3

2

Use:

INSERT INTO products 
  (product_id, product_type, created_dt, end_dt) 
VALUES 
  (11, 
   '1', 
   '2010-10-08 00:11:10', 
    DATE_ADD(STR_TO_DATE('2010-10-08 00:11:10', '%Y-%m-%d %H:%i:%s'), INTERVAL 59 MINUTE)
  )

On MySQL 5.1.49, it wouldn't add/subtract because it wasn't implicitly converting the string to a DATETIME data type. So I had to use STR_TO_DATE to explicitly convert the string into a DATETIME. Otherwise, MySQL workbench returns BLOB...

Sign up to request clarification or add additional context in comments.

4 Comments

@dave: You're welcome - I wouldn't have caught the issue if I wasn't testing it.
Actually, this seemed to have subtracted 59 seconds instead of added
@dave: Corrected - I wondered why the title said "add", but your example was subtraction.
Duh, hence the DATE_SUB - I've changed it to DATE_ADD
0

Use timestamp for your database.. like

$date = time();

and save this variable as a value in your database.

If you want to retrieve a date in date format then use this time stamp in

time('M-Y-D',timestamp);

it will format the timestampin date format or format u put in the function...

check PHP mannual for time() here

Comments

0

Try:

INSERT INTO products 
 (product_id, product_type, created_dt, end_dt) 
VALUES 
 ('11', '1', '2010-10-08 00:11:10', '2010-10-08 00:11:10' - INTERVAL 59 Minute)

1 Comment

'2010-10-08 00:11:10' - INTERVAL 59 Minute returns BLOB in MySQL Workbench, against MySQL 5.1.49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.