How can I get the current timestamp using a mysql query?
Depends on which kind you're looking for.
The current integer Unix Timestamp (1350517005) can be retrieved like so:
SELECT UNIX_TIMESTAMP();
MySQL often displays timestamps as date/time strings. To get one of those, these are your basic options (from the MySQL Date & Time reference):
SELECT CURRENT_TIMESTAMP;
SELECT CURRENT_TIMESTAMP();
SELECT NOW();
CURRENT_TIMESTAMP is standard SQL and works on SQL server, Oracle, MySQL, etc. You should try to keep to the standard as much as you can.
Select current_timestamp;
just use NOW()
Full reference: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
Related
this query is saving complete date and time. but i want to save only time not date in database. is there any query to do this?
update table set current_time=now();
Your column must be set to either DATETIME or TIMESTAMP.
If you use the TIME type then your query would work as expected.
If you are using any other type of column then you could use CURTIME() method or CAST(column AS TIME) as mentioned by other answers, however this would use more space on disk, and make for much slower queries if you use to select, and prevent you from various operators:
e.g. SELECT * FROM table WHERE current_time<'12:00'
You can see more information about the different DATE column types here: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-types.html
Note that the CURTIME() method is not a standard SQL function, so this would only work on MySql
U can use CONVERT (time, SYSDATETIME()) as the value.
This automates your process without using Current_time=now();
INSERT INTO table SET current_time = CONVERT (time, SYSDATETIME());
You can also use curtime();
INSERT INTO table SET current_time = curtime();
Credits: Salmaan C
I've already exported data from an Oracle 11g instance as insert statements. They need to go into a MySQL 5.5 instance. Having trouble with the TIMESTAMP conversion; I know I can use the TO_TIMESTAMP function inline with INSERT statements in MySQL; however, am unsure as to the correct flags to use. As follows below, in linear order, is: the Oracle timestamp statement and my attempt at a MySQL compatible statement. Obviously it's not working (in that MySQL states there's a syntax error).
Oracle:
TO_TIMESTAMP('12/22/2015 5:08:59.245837 PM','fmMMfm/fmDDfm/YYYY fmHH12fm:MI:SS.FF AM')
MySQL:
TO_TIMESTAMP('12/22/2015 5:08:59.245837 PM','%m/%d/%Y %h:%i:%s')
What am I missing beyond flags for microseconds and AM/PM?
FOR MYSQL:
STR_TO_DATE('2/6/2015 5:20:43.000000 AM','%c/%d/%Y %l:%i:%s.%f %p')
assumed you export from SQL Developer.
before you export the whole data, change your datetime select query with :
TO_CHAR(COLUMN_NAME, 'YYYY-MM-DD HH24:MI:SS') as XXX
it will produce datetime with MySQL format. so when you get the insert query, you can run directly at MySQL
if you want the date and the timestamp of oracle to be compatible with MySql just alter you session
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'
ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH:MI:SS.FF'
it is better to alter session
I'm using the following query in Oracle, I need the same in MySQL also.
select sys_extract_utc(systimestamp) into currGMTDate from dual;
I tried with CURRENT_TIMESTAMP instead of systimestamp
but the output that I got was a different one. How can I get the same result in MySQL for the above Oracle query?
I tried with the below query and it worked for me
select date_format(CONVERT_TZ( current_timestamp, ##session.time_zone, '+00:00' ),'%d-%m-%Y %r')
I have a table with a DATETIME column.
I would like to SELECT this datetime value and INSERT it into another column.
I did this (note: '2011-12-18 13:17:17' is the value the former SELECT gave me from the DATETIME field):
UPDATE products SET former_date=2011-12-18 13:17:17 WHERE id=1
and get
1064 - You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version
for the right syntax to use near '13:17:17 WHERE itemid=1' at line 1
Ok, I understand it's wrong to put an unquoted string in there, but is DATETIME just a string in the first place?
What do I put in there?
All I want is reliably transfer the existing value over to a new datetime field...
EDIT:
The reason I ask is: I have this special definition, DATETIME, and somehow I thought it gives me some security and other advantages when handling dates. Now it seems it is simply a specialized VARCHAR, so to speak.
Thanks for your answers, it seems this is indeed the intended behaviour.
According to MySQL documentation, you should be able to just enclose that datetime string in single quotes, ('YYYY-MM-DD HH:MM:SS') and it should work. Look here: Date and Time Literals
So, in your case, the command should be as follows:
UPDATE products SET former_date='2011-12-18 13:17:17' WHERE id=1
Try
UPDATE products SET former_date=20111218131717 WHERE id=1
Alternatively, you might want to look at using the STR_TO_DATE (see STR_TO_DATE(str,format)) function.
for MYSQL try this
INSERT INTO table1(myDatetimeField)VALUES(STR_TO_DATE('12-01-2014 00:00:00','%m-%d-%Y %H:%i:%s');
verification-
select * from table1
output- datetime= 2014-12-01 00:00:00
If you don't need the DATETIME value in the rest of your code, it'd be more efficient, simple and secure to use an UPDATE query with a sub-select, something like
UPDATE products SET t=(SELECT f FROM products WHERE id=17) WHERE id=42;
or in case it's in the same row in a single table, just
UPDATE products SET t=f WHERE id=42;
Basically I am trying to convert a date field from my database into a number of seconds old. I used UNIX_TIMESTAMP on our old MySQL database and am looking for an equivalent function in PostgreSQL.
Thanks in advance
SELECT extract(epoch FROM your_datetime_column)
FROM your_table
More details in the manual:
http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
This:
select CURRENT_TIMESTAMP - '1970-01-01';
Will give you an interval type. You can get seconds out of that interval result if you need it.
I am trying to convince existing MySQL queries to work, this is what I have, seem to be working fine to me.
CREATE OR REPLACE FUNCTION UNIX_TIMESTAMP(date_field timestamp with time zone) RETURNS integer
AS $$
BEGIN
RETURN extract(epoch FROM date_field);
END;
$$ LANGUAGE plpgsql;