0

I'm reading a timestamp from a mysql table using an OdbcDataReader. When I look at the data in the table it is in the format 2013-09-12 11:11:09. But the reader seems to read it in the format 12/09/2013 11:11:09.

I then try to insert this into another mysql table but receive the error:

Incorrect datetime value: '12/09/2013 11:11:09' for column 'timestamp' at row 1

How can I sort out this difference in formatting? Should I be referencing some Unix timestamp value somehow?

2
  • You can refer stackoverflow.com/questions/8551940/… for your answer Commented Sep 12, 2013 at 16:36
  • @VijayPote: Both of the answers on that question are sticking-plaster solutions. Fixing the underlying issue of including a string conversion unnecessarily is a better approach IMO. Commented Sep 12, 2013 at 18:28

2 Answers 2

3

The data shouldn't be in the table in any text format. It's just a date and time.

You'll see the format when you convert the data to a string - which you should do as rarely as possible. In particular, when you're inserting the data into a different table, you shouldn't use a formatted value at all - you should use a DateTime in parameterized SQL.

Basically, unless you really need a string representation of the data, you should keep it in the "native" representation (DateTime in this case). Every time you have a conversion to or from text, that's an opportunity for failure. Dates and times are hard enough with time zones etc, without extraneous conversions getting involved.

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

3 Comments

Yes, I guess it's because I'm converting to string that's causing the problem. I'm doing the following, where reader contains the data read from the mysql table. cmd.CommandText = "insert into mytable set timestamp='" + reader["timestamp"] + "' where id=1"; This must be wrong - how should I be doing it?
@Jon: You should be using parameterized SQL, as I said, e.g. INSERT INTO MyTable SET timestamp = @timestamp and then setting the @timestamp parameter. Look at the documentation for MySqlCommand.Parameters for examples.
@Jon: Excellent. Use it everywhere. See bobby-tables.com for important reasons beyond just date conversions (and humour).
1

How are you looking at the data "in the table"? I'm not familiar with the MySQL implementation, but with Oracle and Sql Server datetime values are stored in an unreadable binary format, and translated to a readable timestamp by the query tool. MySQL is likely doing something similar.

try to insert this into another mysql table

If you care about format when you're inserting the data, you're doing something really bad. That's a strong indication you're using a technique that will be vulnerable to sql injection attacks, rather than parameterized queries. If you use parameterized queries, you assign a C# datetime type to the query parameter value directly, and the ADO.Net object handles any formatting you need. At that point, anything you can successfully DateTime.Parse() or DateTime.TryParse() becomes a valid input for your query.

1 Comment

I'm looking at the data using a GUI program, specifically sqlyog. I will look into parameterized queries.

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.