Solved

how to set datetime now() in telnet

Posted on 2003-10-29
12
1,009 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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
 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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