Converting integers to datetime

This is a discussion on Converting integers to datetime within the MySQL Database forums, part of the Database Forums category; I have the following fields in a database: year integer(4) not null default '0' month integer(2) not null ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-03-2005
Peter
 
Posts: n/a
Default Converting integers to datetime

I have the following fields in a database:

year integer(4) not null default '0'
month integer(2) not null default '0'
day integer(2) not null default '0'
hour integer(2) not null default '0'
minute integer(2) not null default '0'

and just added (using alter table) the following blank field:

date datetime not null default '0'

I want to use (year, month, day, hour, minute) to fill in the datetime field
using the MySQL monitor. How can this be done?

Thanks!
Pete
Reply With Quote
  #2 (permalink)  
Old 11-03-2005
Giuseppe Maxia
 
Posts: n/a
Default Re: Converting integers to datetime

Peter wrote:
> I have the following fields in a database:
>
> year integer(4) not null default '0'
> month integer(2) not null default '0'
> day integer(2) not null default '0'
> hour integer(2) not null default '0'
> minute integer(2) not null default '0'
>
> and just added (using alter table) the following blank field:
>
> date datetime not null default '0'
>
> I want to use (year, month, day, hour, minute) to fill in the datetime field
> using the MySQL monitor. How can this be done?
>
> Thanks!
> Pete


update tablename
set `date` = concat(
lpad( `year`, 4,'0' ), '-',
lpad( `month`, 2,'0' ), '-',
lpad( `day`, 2,'0' ), ' ',
lpad( `hour`, 2,'0' ), ':',
lpad( `minute`, 2,'0' ), ':00'
);

Side note: Don't call your field 'date'. It's a reserved word.
Even though MySQL is forgiving, you can face subtle errors if you
are not extra careful.

ciao
gmax

--
_ _ _ _
(_|| | |(_|><
_|
http://gmax.oltrelinux.com
Reply With Quote
  #3 (permalink)  
Old 11-03-2005
Peter Jay Salzman
 
Posts: n/a
Default Re: Converting integers to datetime

Giuseppe Maxia <gmax_@_cpan_._org> wrote:
> Peter wrote:
>> I have the following fields in a database:
>>
>> year integer(4) not null default '0'
>> month integer(2) not null default '0'
>> day integer(2) not null default '0'
>> hour integer(2) not null default '0'
>> minute integer(2) not null default '0'
>>
>> and just added (using alter table) the following blank field:
>>
>> date datetime not null default '0'
>>
>> I want to use (year, month, day, hour, minute) to fill in the datetime field
>> using the MySQL monitor. How can this be done?
>>
>> Thanks!
>> Pete

>
> update tablename
> set `date` = concat(
> lpad( `year`, 4,'0' ), '-',
> lpad( `month`, 2,'0' ), '-',
> lpad( `day`, 2,'0' ), ' ',
> lpad( `hour`, 2,'0' ), ':',
> lpad( `minute`, 2,'0' ), ':00'
> );
>
> Side note: Don't call your field 'date'. It's a reserved word.
> Even though MySQL is forgiving, you can face subtle errors if you
> are not extra careful.
>
> ciao
> gmax


Hmmm. Never heard of lpad. I'll go look that up now.

Point taken with the field name. You're absolutely right; I'll change it.

A big thanks for the reply! :)

Pete
Reply With Quote
  #4 (permalink)  
Old 11-03-2005
Christian Kirsch
 
Posts: n/a
Default Re: Converting integers to datetime

Peter schrieb:
> I have the following fields in a database:
>
> year integer(4) not null default '0'
> month integer(2) not null default '0'
> day integer(2) not null default '0'
> hour integer(2) not null default '0'
> minute integer(2) not null default '0'
>
> and just added (using alter table) the following blank field:
>
> date datetime not null default '0'
>
> I want to use (year, month, day, hour, minute) to fill in the datetime field
> using the MySQL monitor. How can this be done?
>


By using the appropriate UPDATE statement, I'd guess. Like so (untested)

UPDATE table
SET date <----------- bad, bad choice for a name.
= concat(year,'-',month,'-',day,' ',hour,':',second)

You might want to read the documentation concerning the use of
reserved names, the date-time and string functions, and the format for
date/time literals. The documentation is available at dev.mysql.com/doc
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:19 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0