Pages

Wednesday, February 23, 2011

How to update only the time part of a date type column in MySQL

I was reading data from the web and there was only date  in this format (01/01/2011). So it came to my database with the time part all zeros. But I at least required fake times instead of all zeros. To do that the query in MySQL is as follows
   
                   update news set news.post_date = TIMESTAMPADD(HOUR,14,news.post_date) where TIME(news.post_date) = TIME("00:00:00");

by the way one more thing, If you are using Hibernet to access your database (As I was doing) dont forget to change the temporal type to TimeStamp. Sample code is as follows

              @Column(name = "post_date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date postDate;

Thats it.

No comments:

Post a Comment