DataGrip-Presto JDBC: Timezone displays incurrect even after setting -Duser.timezone=+0800 - datagrip

Datagrip post
Current time is 2018-11-27 14:52:11 timezone=+0800
But
select now() is 2018-11-27 06:52:11 (Wrong)
-- result: 2018-11-27 00:00:07, 2018-11-27 06:52:37 (Wrong)
select
min(sale_time),
max(sale_time)
from sales
where sale_time > current_date and sale_time <= now()
After setting VM options to -Duser.timezone=+0800
select now() is 2018-11-27 06:52:11 (Wrong)
-- result: 2018-11-27 00:00:07, 2018-11-27 14:52:57 (Right)
select
min(sale_time),
max(sale_time)
from sales
where sale_time > current_date and sale_time <= now()
Any solutions?

In your case, there is no possibility to display date in other time zone than UTC. There is an feature request to implement custom data displaying in DataGrip: https://youtrack.jetbrains.com/issue/DBE-6667

Related

Oracle timestamp and Interval

I have a requirement to get records from a table that are systimestamp > LAST_UPDATE_TS + 5 minutes interval
Could you please help with query?
ORACLE DB - 11G Version.
I have tried this as below but not working as expected.
SYSTIMESTAMP : 11-MAR-20 06.06.00.070695 AM -05:00
LAST_UPDATE_TS : 11-MAR-20 06.05.50.781167 AM
After applying this condition, systimestamp > LAST_UPDATE_TS + INTERVAL '5' MINUTE,
I expect no data should return, but still i get rows that doesn't satisfy condition.
You are comparing SYSTIMESTAMP which is a TIMESTAMP WITH TIME ZONE data type to a TIMESTAMP(6) data type; this requires a conversion to a time zone.
If you use:
SELECT SESSIONTIMEZONE FROM DUAL;
The you can see the time zone your session is using.
On db<>fiddle, the default is UTC (+00:00) and running:
SELECT DUMMY AS query1
FROM DUAL
WHERE TIMESTAMP '2020-03-11 06:06:00.070695 -05:00' > TIMESTAMP '2020-03-11 06:05:50.781167' + INTERVAL '5' MINUTE
Outputs:
| QUERY1 |
| :----- |
| X |
Since 2020-03-11 06:06:00.070695 -05:00 is greater than 2020-03-11 06:10:50.781167 +00:00.
If you change the session time zone:
ALTER SESSION SET TIME_ZONE = '-05:00';
and run the same query again (a different column alias was used to prevent caching):
SELECT DUMMY AS query2
FROM DUAL
WHERE TIMESTAMP '2020-03-11 06:06:00.070695 -05:00' > TIMESTAMP '2020-03-11 06:05:50.781167' + INTERVAL '5' MINUTE
Then the output has zero rows:
| QUERY2 |
| :----- |
If you want to manually set the timezone in the conversion then you can use the FROM_TZ function:
SELECT DUMMY AS query3
FROM DUAL
WHERE TIMESTAMP '2020-03-11 06:06:00.070695 -05:00' > FROM_TZ( TIMESTAMP '2020-03-11 06:05:50.781167', '-05:00' ) + INTERVAL '5' MINUTE
Which, again, outputs zero rows:
| QUERY3 |
| :----- |
db<>fiddle here
SYSTIMESTAMP returns a TIMESTAMP WITH TIME ZONE value which is compared with a TIMESTAMP value.
Actually Oracle is doing this:
SYSTIMESTAMP > FROM_TZ(LAST_UPDATE_TS + INTERVAL '5' MINUTE, SESSIONTIMEZONE)
Comparison itself is performed on UTC times.
SYSTIMESTAMP is returned in the time zone of database server's operating system. If this time zone is equal to your current SESSIONTIMEZONE then the condition works as expected.
Either change your session time zone to the time zone of database server's operating system or try this one:
LOCALTIMESTAMP > LAST_UPDATE_TS + INTERVAL '5' MINUTE
which does not utilize time zones at all.
After checking few forums and other stackoverflow pages, I tried this and it seams to be working now.
select *
from table
where cast(systimestamp as TIMESTAMP) > (lst_updt_ts + interval '3' minute);
Doing it the old way?
systimestamp -5/(60*24) > LAST_UPDATE_TS

Oracle SYSDATE-1 not returning 24 hour range

I have a table with a timestamp column. My query has a WHERE clause
AND date_created.TIMESTAMP_DATA >= (SYSDATE - 1)
This should return any timestamp which is now-24 hours but I do not get records done the previous day, only the day of (today). I checked that the db SYSDATE is accurate and manipulating date_created to today returns the results, however if date_create is yesterday #5pm, it does not return
Please try changing query as:
AND date_created.TIMESTAMP_DATA >= (SYSTIMESTAMP - 1)
Abhi
Turned out to be a timezone issue. this solved it
AND date_created.TIMESTAMP_DATA >= (SYSTIMESTAMP AT TIME ZONE 'PST'- INTERVAL '1' DAY)
Edited to include - INTERVAL '1' DAY instead of -1

to display date in the format of ‘YYYY-MM-DD’ when the date is given in the form 1-JAN-2011?

I have to write a query to display schedule id and date in the format of ‘YYYY-MM-DD’.
can anyone help me with it
travel date is this
TRAVEL_DA
---------
- 09-MAY-16
- 17-MAR-16
- 26-AUG-16
- 30-APR-16
- 17-AUG-16
and I want to get this output
FORMATTEDD
--- ----------
- 2016-05-09
- 2016-03-17
- 2016-08-26
- 2016-04-30
- 2016-08-17
SELECT DATE_FORMAT(STR_TO_DATE(TRAVEL_DA,'%d-%b-%y'),'%Y-%m-%d') FROM TABLE;
For Oracle you can try TO_CHAR & TO_DATE
SELECT TO_CHAR(TO_DATE(TRAVEL_DA,'DD-MON-YY'),'YYYY-MM-DD') FROM TABLE;
Sample query:
SELECT TO_CHAR(TO_DATE('09-MAY-16','DD-MON-YY'),'YYYY-MM-DD') FROM DUAL;
Here sample query for same.
SELECT DATE_FORMAT(STR_TO_DATE('09-MAY-16','%d-%b-%y'),'%Y-%m-%d')
Here I had added functionDATE_FORMAT and STR_TO_DATE.
Using this two function you can resolve your issue.
If you have a lot of select to do you can set the date format for your session
ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD';
And you can remove the TO_CHAR from your requests
SELECT TO_DATE(TRAVEL_DA,'DD-MON-YY') FROM TABLE;
In addition to the above solution this will also work in Oracle database using Symfony Doctrine with the following extensions:
https://github.com/beberlei/DoctrineExtensions
TO_CHAR: DoctrineExtensions\Query\Oracle\ToChar
TO_DATE: DoctrineExtensions\Query\Oracle\ToDate
Once loaded in doctrine.yaml the following SELECT statement will also work:
SELECT
TO_CHAR(TO_DATE(COLUMNNAME,'DD-MON-YY'),'YYYY-MM-DD') AS FIELDNAME
FROM
Connection:Entity
Hi you can try below query :
SELECT DATE_FORMAT (DATE_COLUMN, '%Y-%m-%d') FROM YOUR_TABLE

How do I update the time in oracle sql?

I need a query to update a time in an appointment date by keeping the date but changing the time.
For example
10-Feb-2016 09:00:00
and i want to change it to 10-Feb-2016 10:00:00.
Update Appointment
set vdate = '10:00:00'
where vdate= '10-Feb-2016'
I get the "0 row has been updated'. Not sure if i'm missing something.
Thanks in advance.
You can use trunc() which sets the time part of a DATE (or TIMESTAMP) to 00:00:00, then add the 10 hours to it:
Update Appointment
set vdate = trunc(vdate) + interval '10' hour
where trunc(vdate) = DATE '2016-02-10'
This would change all rows that have a date 2016-02-10. If you only want to do that for those that are at 09:00 (ignoring the minutes and seconds) then just add one hour to those rows
Update Appointment
set vdate = trunc(vdate) + interval '1' hour
where trunc(vdate, 'hh24') = timestamp '2016-02-10 09:00:00'
trunc(vdate, 'hh24') will set the minutes and seconds of the date value to 00:00, so that the comparison with 2016-02-10 09:00:00 works properly.
Unrelated, but: do not rely on implicit data type conversion. '10-Feb-2016' is a string value, not a DATE literal. To specify a date either use an ANSI DATE literal (as I have done in the above statement) or use the to_date() function with a format mask to convert a string literal to a proper date value.
Your statement is subject to the evil implicit data type conversion and will fail if the SQL client running the statement uses a different NLS setting (it will fail on my computer for example)
If what you want to do is add an hour to a date, then you can do:
Update Appointment
set vdate = vdate + 1/24
where vdate = to_date('10/02/2016 09:00', 'dd/mm/yyyy hh24:mi');
since in Oracle, date differences are measured in number of days, and an hour is 1/24th of a day.
If what you want to do is specify an exact time (e.g. to 10:25:48), then you could do the following instead:
Update Appointment
set vdate = trunc(vdate) + 10/24 + 25/(24*60) + 48/(24*60*60)
where vdate = to_date('10/02/2016 09:00', 'dd/mm/yyyy hh24:mi');
Bear in mind that these updates will update all rows that have a date of 10th Feb 2016 at 9am. You'd need to change your query's where clause if you wanted to specify a more specific row or set of rows.
Try like this.
UPDATE MyTable
SET MyDate = DATEADD(HOUR, 4, CAST(CAST(MyDate AS DATE) AS DATETIME))
or
UPDATE MyTable
SET MyDate = DATEADD(HOUR, 4, CAST(FLOOR(CAST(MyDate AS FLOAT)) AS DATETIME))

Oracle current_timestamp to seconds conversion

We are using Oracle database.
In our table timestamp is stored as seconds since 1970, how can I convert the time stamp obtained through current_timestamp() function to seconds
This would do it:
select round((cast(current_timestamp as date) - date '1970-01-01')*24*60*60) from dual
Though I wouldn't use current_timestamp if I was only interested in seconds, I would use SYSDATE:
select round((SYSDATE - date '1970-01-01')*24*60*60) from dual
Maybe not completely relevant. I had to resolve other way around problem (e.g. Oracle stores timestamp in V$RMAN_STATUS and V$RMAN_OUTPUT) and I had to convert that to date/timestamp. I was surprised, but the magic date is not 1970-01-01 there, but 1987-07-07. I looked at Oracle's history and the closest date I can think of is when they ported Oracle products to UNIX. Is this right?
Here's my SQL
SELECT /*+ rule */
to_char(min(stamp)/(24*60*60) + date '1987-07-07', 'DD-MON-YYYY HH24:MI:SS') start_tm
, to_char(to_char(max(stamp)/(24*60*60) + date '1987-07-07', 'DD-MON HH24:MI:SS')) end_tm
FROM V$RMAN_STATUS
START WITH (RECID, STAMP) =
(SELECT MAX(session_recid),MAX(session_stamp) FROM V$RMAN_OUTPUT)
CONNECT BY PRIOR RECID = parent_recid ;
I needed to send timestamp to GrayLog via GELF from Oracle DB. I tried different versions and solutions but only one worked correctly.
SQL:
SELECT REPLACE((CAST(dat AS DATE) - TO_DATE('19700101', 'YYYYMMDD')) * 86400 + MOD(EXTRACT(SECOND FROM dat), 1), ',', '.') AS millis
FROM (SELECT SYSTIMESTAMP AT TIME ZONE 'GMT' AS dat FROM dual)
The result for Systmiestamp
2018/12/18 19:47:29,080988 +02:00
will be
1545155249.080988

Resources