0

I have the following column value which I am converting and storing in another column

Nov 22 2014 00:00:00 AM

Now I am using the following query to convert it to Date format and store in another column

UPDATE DataNov2014 SET Datee = str_to_date(Date,'%d %b %Y %H:%i:%s');

But I am getting the following exception

Exception in thread "main" java.sql.SQLException: Incorrect datetime value: 'Nov 22 2014 00:00:00 AM' for function str_to_date

Is there any mistake in my query/date format ??
Any help would be appreciated, Thanks

Gagan93
  • 1,826
  • 2
  • 25
  • 38
  • Is the Hour:min:sec is in 24 hour format or 12 hour format ? – Abhik Chakraborty Nov 22 '14 at 09:19
  • SO is the issue in mysql `00:00:00 AM` is invalid. You can have `00:00:00 AM` only in 24 hour format not in 12 hours format. So it needs to be in 24 hour format and the str_to_date should be as `str_to_date(Date,'%b %d %Y %H:%i:%s')` – Abhik Chakraborty Nov 22 '14 at 09:43
  • %r Time, 12-hour (hh:mm:ss followed by AM or PM).... This is the format of a 12 hr time in mysql @AbhikChakraborty, right ? – Gagan93 Nov 22 '14 at 09:52
  • Yes its allowed but you can not have `00:00:00` its invalid in 12 hour format it has to be `12:00:00 AM` – Abhik Chakraborty Nov 22 '14 at 09:53
  • Thanks for pointing that silly error. Now my Time is "Nov 22 2014 00:00:00" and Update query says "STR_TO_DATE(Date,'%d %b %Y %T')". Why this is still giving the same exception ? – Gagan93 Nov 22 '14 at 10:01
  • I think it should be `str_to_date(Date,'%b %d %Y %T')`, you has `%d %b ...` – Abhik Chakraborty Nov 22 '14 at 11:06

3 Answers3

0

tyr this

UPDATE DataNov2014 SET Date= to_char(Date,'mon dd yyyy mm:ss:hh');
suresh manda
  • 659
  • 1
  • 8
  • 25
0

I suggest you use a PreparedStatement and a Date bind parameter. Also, you can use try-with-resources. Putting it all together, something like

String sql = "UPDATE DataNov2014 SET Datee = ?";
DateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
try (PreparedStatement ps = conn.prepareStatement(sql)) {
    Date theDate = sdf.parse("11-22-2014 00:00:00");
    ps.setDate(1, new java.sql.Date(theDate.getTime()));
    int count = ps.executeUpdate();
    if (count > 0) {
        System.out.printf("Query updated %d rows.%n", count);
    } else {
        System.out.println("Query didn't update any rows");
    }
} catch (Exception e) {
    e.printStackTrace();
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I have never used prepared statement, are you sure that this will solve my problem, because the exception I am getting is in the source date format ! – Gagan93 Nov 22 '14 at 09:46
  • @Gagan93 I would recommend you start using prepared statements. Also, I am positive that if you have a Java `Date` Object using `setDate()` will help (you aren't converting from a `String` to a `Date`, and the JDBC driver knows how to store `Date`(s)). – Elliott Frisch Nov 22 '14 at 09:48
0

Assumes your field Datee datatype is Datetime

UPDATE DataNov2014 SET Datee = str_to_date('Nov 22 2014 00:00:00 AM','%M %d %Y %H:%i:%s');

fortune
  • 3,361
  • 1
  • 20
  • 30