Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert US date format to MYSQL format in INSERT query

Tags:

php

mysql

I have following insert query:

INSERT INTO `bid`.`bdate` (`id`, `bid`, `odate`) VALUES (NULL, '1', STR_TO_DATE('02-27-2011 17:58','%Y-%m-%d %H:%i:%s'))

But it is giving me errors. Can some one guide me what Iam doing wrong and how it can be rectified.

Thanks in advance

like image 706
Simpanoz Avatar asked Feb 25 '11 06:02

Simpanoz


People also ask

How do I convert a date from one format to another in MySQL?

In a MySQL database, the DATE_FORMAT() function allows you to display date and time data in a changed format. This function takes two arguments. The first is the date/datetime to be reformatted; this can be a date/time/datetime/timestamp column or an expression returning a value in one of these data types.

How do I insert date in mm/dd/yyyy format in MySQL?

Example: For converting datetime to format – dd:mm:yyyy, you can use the following query: SELECT DATE_FORMAT('2020-03-15 07:10:56.123', '%d:%m:%Y');

What is the format to insert date in MySQL?

MySQL Date Data Types DATE - format YYYY-MM-DD. DATETIME - format: YYYY-MM-DD HH:MI:SS. TIMESTAMP - format: YYYY-MM-DD HH:MI:SS. YEAR - format YYYY or YY.


2 Answers

You have the date formats in the wrong place. Change your query to:

STR_TO_DATE('02-27-2011 17:58','%m-%d-%Y %H:%i'))
like image 155
The Scrum Meister Avatar answered Oct 11 '22 20:10

The Scrum Meister


You dont have a place holder for %s.

try this.

INSERT INTO `bid`.`bdate` (`id`, `bid`, `odate`) VALUES (NULL, '1', STR_TO_DATE('02-27-2011 17:58:00','%Y-%m-%d %H:%i:%s'))
like image 33
rdp Avatar answered Oct 11 '22 18:10

rdp