Solved

how to set datetime now() in telnet

Posted on 2003-10-29
12
1,001 Views
Last Modified: 2008-03-10
i need to do this:

mysql> alter table ApReq modify TimeStamp datetime now();
 i can't get the now() part -
0
Comment
Question by:phillystyle123
  • 5
  • 5
  • 2
12 Comments
 
LVL 20

Expert Comment

by:ikework
ID: 9645472
in mysql timestamp is a datatype itsself, so you can declare a field, which is updated
with every insert and update, depending on the running mode of mysql
(for further details llook at http://www.mysql.com/doc/en/DATETIME.html )

create table your_table(the_timstamp_field timestamp);


i guess, what you wanted to do is to set the default-value of your field to now(), to do it try this:

alter table ApReq modify TimeStamp datetime default now();

not sure, if it works, cauz TimeStamp is a keyword of mysql... good luck


hope this helps :)

maik
0
 
LVL 17

Expert Comment

by:Squeebee
ID: 9645551
But then should the default be the current value of now() or always be the current time?
Like ikework said, if you are trying to create a timestamp of the updates on the row use a TIMESTAMP type column, it will update to the current time with every update of the row.
0
 

Author Comment

by:phillystyle123
ID: 9645593
it doesn't work.  I have managed to successfully change the field's type to datetime though so I don't think TimeStamp is a problem.  it's just the now() part that doesn't seem to be working.
0
 

Author Comment

by:phillystyle123
ID: 9645609
i think i need to use datetime because every time i'm updating some table info using odbc linked to access and with timestamp everytime i update something it changes the date to the current date.  
0
 
LVL 17

Accepted Solution

by:
Squeebee earned 50 total points
ID: 9645678
That is what a TIMESTAMP field does, do you just want a timestamp of the time the row was first created?

You could still use timestamp, and in any subsequent UPDATE queries always

SET timestampcol = timestampcol

Which will keep the timestamp column the same so it does not update.

Otherwise you can always use NOW() in the INSERT queries.
0
 

Author Comment

by:phillystyle123
ID: 9645699
so there is no way to set datetime default to now() in mysql?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 17

Expert Comment

by:Squeebee
ID: 9645765
Well, what you can do is create two TIMESTAMP fields, call the first one update time and the second one create time. As you can see in the example below, when you have more than one timestamp column, they will both catch the current time during row creating, but only the first one catches the new time during UPDATE queries.

---------------------------------------------------------------------------------

mysql> create table temptime(time1 timestamp, time2 timestamp);
Query OK, 0 rows affected (0.13 sec)

mysql> insert into temptime VALUES(NULL,NULL);
Query OK, 1 row affected (0.02 sec)

mysql> insert into temptime VALUES(NULL,NULL);
Query OK, 1 row affected (0.00 sec)

mysql> insert into temptime VALUES(NULL,NULL);
Query OK, 1 row affected (0.00 sec)

mysql> select * from temptime;
+---------------------+---------------------+
| time1               | time2               |
+---------------------+---------------------+
| 2003-10-29 14:35:59 | 2003-10-29 14:35:59 |
| 2003-10-29 14:36:02 | 2003-10-29 14:36:02 |
| 2003-10-29 14:36:03 | 2003-10-29 14:36:03 |
+---------------------+---------------------+
3 rows in set (0.00 sec)

mysql> update temptime set time1 = now();
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select * from temptime;
+---------------------+---------------------+
| time1               | time2               |
+---------------------+---------------------+
| 2003-10-29 14:36:37 | 2003-10-29 14:35:59 |
| 2003-10-29 14:36:37 | 2003-10-29 14:36:02 |
| 2003-10-29 14:36:37 | 2003-10-29 14:36:03 |
+---------------------+---------------------+
3 rows in set (0.00 sec)
0
 

Author Comment

by:phillystyle123
ID: 9646081
Squeebee  - thanks for the code but i already have the thing up and running so i'd rather not go to all this trouble if i can just do datetime now().
0
 
LVL 17

Expert Comment

by:Squeebee
ID: 9646188
Oh, I should have mentioned that no, you can't.
0
 

Author Comment

by:phillystyle123
ID: 9646214
why not?
0
 
LVL 20

Expert Comment

by:ikework
ID: 9646316
i guess mysql doesn't think this case doesn't worth to be implemented,
i can understand that, cauz you can simply do it in your insert statement,
why don't you do it this way?

insert into your_table(datefield)values(now());
0
 
LVL 17

Expert Comment

by:Squeebee
ID: 9646331
MySQL did not anticipate the need I suppose. Between TIMESTAMP for auto updated fields and DATETIME for static fields that have most of their bases covered, and the remaining users either use two TIMESTAMP fields like I suggested, or INSERT NOW() like ikework suggested.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now