I have a table containing datetime field. I want to select records with a condition that only those records that has datetime value above current IST time.
Example
select * from mytable where dt > current_ist_datetime
current_ist_datetime is what i do not know how to use. CONVERT_TZ() with NOW() can give it. But i do not know what timezone mysql uses. that is, i do not know second parameter that i should pass to CONVERT_TZ()
You can retrieve the timezone setting for the current MySQL session with a SQL statement:
mysql> SELECT ##session.time_zone;
If the DBA has not set the timezone on MySQL, it will default to a value of SYSTEM, which represents the timezone on the operating system.
http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
FOLLOWUP:
In a predicate as in your example SQL statement:
select * from mytable where dt > current_ist_datetime
(given dt and current_ist_datetime of datatype DATETIME)
the comparison of the values will be independent of any timezone conversion, since there is no no timezone information associated with a DATETIME value.
That is, the value returned from a column of datatype DATETIME is not affected by the time_zone setting of the MySQL server (SELECT ##global.time_zone) or of the MySQL session (SELECT ##session.time_zone).
The value returned by the NOW() function, however, will be impacted by the time_zone setting of the session.
To get that returned in IST, ensure the time_zone for the session is specified correctly, e.g.
SET VARIABLES time_zone = "+05:30"
(N.B. If the session is retrieved from and returned from a connection pool, the other users of the pool may not be expecting a different time_zone;...)
(NOTE: the above entirely ignores the confusion introduced by the JDBC driver, caused by the "impedance mismatch" (differences) between MySQL's implementation of DATETIME datatype, and Java implementation of Date object. If you are passing DATETIME values across a JDBC connection, that's whole 'nother ball of confusion.)
MySQL uses timezone in two formats; i.e. you can either use 'Asia/Calcutta' or '+05:30' (for IST). But I would suggest you use the latter one.
Timezone for UTC is '+00:00'. So you can use following query provided that dt is in UTC & current_ist_datetime is in IST
select * from mytable where dt > CONVERT_TZ(current_ist_datetime,'+05:30','+00:00')
I am using this query in my project where I paas current_ist_datetime & +05:30 from client(in IST) to my server(in UTC) & it works like a charm.
If you use NOW() mysql will select current date time of your system's timezone
select * from mytable where dt > CONVERT_TZ(NOW(),'+05:30','+00:00')
You can modify these queries according to your need.
Related
We are storing datetime in a column on our MySQL database, formatted in TEXT, but when our datetime is supposed to look like below:
'xxxx-xx-xx 00:00:00'
The time is deleted or not show on our datetime, and therefore our datetime, at that specific time, only contains the date:
'xxxx-xx-xx'
What we want is first of all to figure out why this is occurring, but for now we need to edit every row, and make sure the datetime is also showing the time. We have tried to change the impacted rows by using this query:
UPDATE table SET TIME(col_datetime) = '00:00:00' WHERE LENGTH(TIME(col_datetime)) = 0;
Above query should update the time on the datetime for col_datetime, where length of time is 0. Unfortunately, we receive an error, and we can't run the query. This is the error:
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 '(time_start) = '00:00:00' WHERE LENGTH(TIME(time_start)) = 0' at line 2
How can we change time on our datetime, where time is not shown?
Don't store dates as strings. Instead, you want to use the datetime datatype: it has a time part, that defaults to 00:00:00 when not specified.
Here is a small conversion script for that purpose:
alter table mytable add col_datetime_new datetime;
update mytable set col_datetime_new = col_datetime;
alter table mytable drop col_datetime;
alter table mytable change column col_datetime_new col_datetime datetime;
This leverages the fact that you are using format YYYY-MM-DD in your string dates, so conversion to datetime is seemless.
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
Assume the following table:
CREATE TABLE IF NOT EXISTS `test` (
`stamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
When I run the following SQL:
SET time_zone = '+00:00';
INSERT INTO `test` (`stamp`) VALUES (CURRENT_TIMESTAMP);
The value in test is my machine's local time instead of UTC.
According to MySQL site
Values for TIMESTAMP columns are converted from the current time zone
to UTC for storage, and from UTC to the current time zone for
retrieval.
What am I doing wrong? I want to store my records with UTC date/time stamp and retrieve them in the user's local time zone.
According to the MySQL 5.5 documentation using the TIMESTAMP data type will default to using the server's time zone unless you change the variable from the client side.
From: http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
Per-connection time zones. Each client that connects has its own time
zone setting, given by the session time_zone variable. Initially, the
session variable takes its value from the global time_zone variable,
but the client can change its own time zone with this statement:
mysql> SET time_zone = timezone;
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