How to select datetime from ORACLE db using IST timezone - sql

Oracle database is hosted in US. Application server hosted in UK.
I am trying to access the oracle application from India, how can I write a query that will show IST time. (Local machine time).
This is an oracle form, so no coding can be done.
Tried below but did not worked
SELECT
TO_CHAR(current_date, 'DD-MON-YYYY HH24:MI')
FROM DUAL

Indian time is +5.5 hours from GMT, SYS_EXTRACT_UTC(SYSTIMESTAMP) gives time in GMT for any Oracle server
select to_char(SYS_EXTRACT_UTC(SYSTIMESTAMP) +
(5.5/24), 'DD-MON-YY HH24:MI:SS') from dual;

First change the timezone of your session:
ALTER SESSION SET TIME_ZONE = 'Asia/Calcutta';
SELECT TO_CHAR(CURRENT_DATE, 'DD-MON-YYYY HH24:MI') FROM dual;
Note, you cannot use the short name, because 'IST' is also used for "Indian Standard Time" and "Iran Standard Time" and "Israel Standard Time" and "Indonesian Standard Time".
Thus Oracle requires either the full name (see IANA time zone database) or UTC Offset, e.g. ALTER SESSION SET TIME_ZONE = '+5:30';. However, this is more risky in case your country uses DST (Daylight Saving Time)

Change your timezone with this first,
systimestamp AT TIME ZONE 'IST'

Related

TO_UTC_TIMESTAMP_TZ not returning Month name [duplicate]

I firstly run the following command, and get the following result:
select to_char(systimestamp, 'YYYY-MM-DD HH24:MI:SS') from dual;
TO_CHAR(SYSTIMESTAM
-------------------
2018-07-10 10:21:40
This is the format I want to have a TIMESTAMP object stored in.
When I convert it back though, it does not come in the format I want:
select to_timestamp(to_char(systimestamp, 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS') from dual;
TO_TIMESTAMP(TO_CHAR(SYSTIMESTA
-------------------------------
10-JUL-18 10.21.40.000000000 AM
In fact, it changes 2018 to be at the end, sets 07 to be "JUL" and the 10 is now at the front. Also the time is separated by dots, has many 0's and an AM.
How can I fix this? I am new to SQL development so I am not sure about the formatting.
Thank you so much
As #Gordon said, timestamps (and dates) are not stored in a format you would recognise Oracle uses an internal representation that you never really need to know about or examine (but it is documented if you're interested in that sort of thing).
When you query a timestamp it is displayed using your client's NLS settings, unless you have a client that overrides those. I can set my session up to match what you are seeing:
alter session set nls_timestamp_format = 'DD-MON-RR HH.MI.SS.FF AM';
select to_char(systimestamp, 'YYYY-MM-DD HH24:MI:SS') from dual;
TO_CHAR(SYSTIMESTAM
-------------------
2018-07-10 15:37:31
select to_timestamp(to_char(systimestamp, 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS') from dual;
TO_TIMESTAMP(TO_CHAR(SYSTIMESTA
-------------------------------
10-JUL-18 03.37.31.000000000 PM
And I can change it see what you want to see:
alter session set nls_timestamp_format = 'YYYY-MM-DD HH24:MI:SS';
select to_timestamp(to_char(systimestamp, 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS') from dual;
TO_TIMESTAMP(TO_CHA
-------------------
2018-07-10 15:37:32
But all you are doing is converting from a timestamp with time zone (which is what systimestamp is) to a string and then back to a timestamp. You are losing the time zone portion, and any fractional seconds; which you could also do with a cast:
select cast(systimestamp as timestamp(0)) from dual;
CAST(SYSTIMESTAMPAS
-------------------
2018-07-10 15:37:32
You can see the timezone and fraction seconds with your default timestamp_tz format:
select systimestamp from dual;
SYSTIMESTAMP
------------------------------------
2018-07-10 15:37:33.776469000 +01:00
and change it with a different alter:
alter session set nls_timestamp_tz_format = 'YYYY-MM-DD HH24:MI:SS.FF3 TZH:TZM';
select systimestamp from dual;
SYSTIMESTAMP
------------------------------
2018-07-10 15:37:34.070 +01:00
Which isn't entirely relevant if you're really talking about storing timestamps in a table, but shows that there are variations.
In your table make the data type timestamp (or timestamp with time zone or timestamp with local time zone), and only worry about formatting the value as a string for presentation to the end user, at the last possible moment.
When you do need to display it, if the display format is important to you then use to_char() with an explicit format mask - do not assume that anyone else running your queries will have the same NLS settings. As you can see, it's easy to change those to modify the output. (Most clients have a way to let you set the defaults so you don't have to do the same alter commands every time you connect; e.g. in SQL Developer, from Tools->Preferences->Database->NLS). If you want to always show the same format then use something like:
select to_char(your_column, 'YYYY-MM-DD HH24:MI:SS') as column_alias
from your_table
where your_column < timestamp '2018-01-01 00:00:00'
which also shows the column value being filtered (as a timestamp still) using a timestamp literal.
This is the format I want to have a TIMESTAMP object stored in.
This is a common misconception - timestamp (and date) data types do not have a format; they are stored in the database as 20 bytes (or 7 bytes for dates) representing:
year (2 bytes),
month, day, hour, minute, integer seconds (1 bytes each),
fractional seconds (4 bytes),
time zone offset hours (1 byte),
time zone offset minutes (1 byte),
other data (including time zone location) (7 bytes)
You can see the bytes using the DUMP function:
SELECT DUMP( your_timestamp_column ) FROM your_table;
The database will operate on these 20 bytes without any format. But that would not be useful to display to you, the user, so the user interface you use to access the database (SQL/Plus, SQL Developer, Toad, Java, C#, etc) will receive those raw bytes from the database and will silently format them into a more intelligible format for you, the user.
What you are actually asking is:
How can I get the application I am using to access the Oracle database to change the default format it uses to display TIMESTAMP data types?
For SQL/Plus (and SQL Developer) you can use the NLS_TIMESTAMP_FORMAT session parameter:
ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
Then:
SELECT SYSTIMESTAMP FROM DUAL;
Will output:
2018-07-10 16:24:53
However, this only sets the default format for your user's current session; other users can set their own parameters and can change the value during their session so you should not rely on this to provide a consistent formatting.
Instead, if you want to have a TIMESTAMP with a particular format then you should convert it to a datatype which can have a format - a string.
SELECT TO_CHAR( SYSTIMESTAMP, 'YYYY-MM-DD HH24:MI:SS' ) FROM DUAL;
Then it does not matter what the user changes their default timestamp format to - your values will always be formatted how you expect.
So based on your replies above it sounds like you may be trying to do something you don't actually need to do.
As Gordon mentioned, timestamps are stored in an internal format so you can do things with the values. If you have the field stored as a timestamp data type you don't need to care how it's formatted in the database, you just need to care about how it looks for your query at the end. You can display a date field using to_char and still use a date range if you have the query built correctly.
select to_char(systimestamp, 'YYYY-MM-DD HH24:MI:SS')
from dual
WHERE systimestamp <= current_timestamp;
I don't think it is possible to to display the timestamp as you've described without using to_char and losing the data type.

How do I get this timestamp in the format I want, Oracle SQL

I firstly run the following command, and get the following result:
select to_char(systimestamp, 'YYYY-MM-DD HH24:MI:SS') from dual;
TO_CHAR(SYSTIMESTAM
-------------------
2018-07-10 10:21:40
This is the format I want to have a TIMESTAMP object stored in.
When I convert it back though, it does not come in the format I want:
select to_timestamp(to_char(systimestamp, 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS') from dual;
TO_TIMESTAMP(TO_CHAR(SYSTIMESTA
-------------------------------
10-JUL-18 10.21.40.000000000 AM
In fact, it changes 2018 to be at the end, sets 07 to be "JUL" and the 10 is now at the front. Also the time is separated by dots, has many 0's and an AM.
How can I fix this? I am new to SQL development so I am not sure about the formatting.
Thank you so much
As #Gordon said, timestamps (and dates) are not stored in a format you would recognise Oracle uses an internal representation that you never really need to know about or examine (but it is documented if you're interested in that sort of thing).
When you query a timestamp it is displayed using your client's NLS settings, unless you have a client that overrides those. I can set my session up to match what you are seeing:
alter session set nls_timestamp_format = 'DD-MON-RR HH.MI.SS.FF AM';
select to_char(systimestamp, 'YYYY-MM-DD HH24:MI:SS') from dual;
TO_CHAR(SYSTIMESTAM
-------------------
2018-07-10 15:37:31
select to_timestamp(to_char(systimestamp, 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS') from dual;
TO_TIMESTAMP(TO_CHAR(SYSTIMESTA
-------------------------------
10-JUL-18 03.37.31.000000000 PM
And I can change it see what you want to see:
alter session set nls_timestamp_format = 'YYYY-MM-DD HH24:MI:SS';
select to_timestamp(to_char(systimestamp, 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS') from dual;
TO_TIMESTAMP(TO_CHA
-------------------
2018-07-10 15:37:32
But all you are doing is converting from a timestamp with time zone (which is what systimestamp is) to a string and then back to a timestamp. You are losing the time zone portion, and any fractional seconds; which you could also do with a cast:
select cast(systimestamp as timestamp(0)) from dual;
CAST(SYSTIMESTAMPAS
-------------------
2018-07-10 15:37:32
You can see the timezone and fraction seconds with your default timestamp_tz format:
select systimestamp from dual;
SYSTIMESTAMP
------------------------------------
2018-07-10 15:37:33.776469000 +01:00
and change it with a different alter:
alter session set nls_timestamp_tz_format = 'YYYY-MM-DD HH24:MI:SS.FF3 TZH:TZM';
select systimestamp from dual;
SYSTIMESTAMP
------------------------------
2018-07-10 15:37:34.070 +01:00
Which isn't entirely relevant if you're really talking about storing timestamps in a table, but shows that there are variations.
In your table make the data type timestamp (or timestamp with time zone or timestamp with local time zone), and only worry about formatting the value as a string for presentation to the end user, at the last possible moment.
When you do need to display it, if the display format is important to you then use to_char() with an explicit format mask - do not assume that anyone else running your queries will have the same NLS settings. As you can see, it's easy to change those to modify the output. (Most clients have a way to let you set the defaults so you don't have to do the same alter commands every time you connect; e.g. in SQL Developer, from Tools->Preferences->Database->NLS). If you want to always show the same format then use something like:
select to_char(your_column, 'YYYY-MM-DD HH24:MI:SS') as column_alias
from your_table
where your_column < timestamp '2018-01-01 00:00:00'
which also shows the column value being filtered (as a timestamp still) using a timestamp literal.
This is the format I want to have a TIMESTAMP object stored in.
This is a common misconception - timestamp (and date) data types do not have a format; they are stored in the database as 20 bytes (or 7 bytes for dates) representing:
year (2 bytes),
month, day, hour, minute, integer seconds (1 bytes each),
fractional seconds (4 bytes),
time zone offset hours (1 byte),
time zone offset minutes (1 byte),
other data (including time zone location) (7 bytes)
You can see the bytes using the DUMP function:
SELECT DUMP( your_timestamp_column ) FROM your_table;
The database will operate on these 20 bytes without any format. But that would not be useful to display to you, the user, so the user interface you use to access the database (SQL/Plus, SQL Developer, Toad, Java, C#, etc) will receive those raw bytes from the database and will silently format them into a more intelligible format for you, the user.
What you are actually asking is:
How can I get the application I am using to access the Oracle database to change the default format it uses to display TIMESTAMP data types?
For SQL/Plus (and SQL Developer) you can use the NLS_TIMESTAMP_FORMAT session parameter:
ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
Then:
SELECT SYSTIMESTAMP FROM DUAL;
Will output:
2018-07-10 16:24:53
However, this only sets the default format for your user's current session; other users can set their own parameters and can change the value during their session so you should not rely on this to provide a consistent formatting.
Instead, if you want to have a TIMESTAMP with a particular format then you should convert it to a datatype which can have a format - a string.
SELECT TO_CHAR( SYSTIMESTAMP, 'YYYY-MM-DD HH24:MI:SS' ) FROM DUAL;
Then it does not matter what the user changes their default timestamp format to - your values will always be formatted how you expect.
So based on your replies above it sounds like you may be trying to do something you don't actually need to do.
As Gordon mentioned, timestamps are stored in an internal format so you can do things with the values. If you have the field stored as a timestamp data type you don't need to care how it's formatted in the database, you just need to care about how it looks for your query at the end. You can display a date field using to_char and still use a date range if you have the query built correctly.
select to_char(systimestamp, 'YYYY-MM-DD HH24:MI:SS')
from dual
WHERE systimestamp <= current_timestamp;
I don't think it is possible to to display the timestamp as you've described without using to_char and losing the data type.

What is the proper way of formatting date in select query Oracle SQL

For suppose if I want sysdate,
SELECT SYSDATE as system_date FROM DUAL;
should output in the following format
14-Feb-2018 T19:50:02+00:00
i.e.,
DD-MMM-YYYY Thh:mm:ss+HH:MM
Assuming you know the date represents UTC and want the +00:00 part to be fixed:
select to_char(sysdate, 'DD-Mon-YYYY "T"HH24:MI:SS"+00:00"') from dual;
TO_CHAR(SYSDATE,'DD-MON-YYYY"T"HH24:
------------------------------------
14-Feb-2018 T20:13:08+00:00
The format model elements are in the documentation. That includes a section on character literals, which I've used for the fixed T and +00:00 parts.
As #mathguy said, this seems a bit unusual; and you might actually to leave the column as a native date and have your application or reporting tool or whatever format it for you. It depends what exactly you're doing, and whether you actually want a string value directly from the query.
As your updated question now doesn't have that pseudo-timezone, it's now even simpler, but the same idea:
select to_char(sysdate, 'DD-Mon-YYYY "T"HH24:MI:SS') from dual;
TO_CHAR(SYSDATE,'DD-MON-YYYY"T
------------------------------
14-Feb-2018 T20:17:50
If you're working with a data type that knows about time zones - i.e. not a plain DATE or TIMESTAMP - you can include those in the formatting using the appropriate model elements:
select to_char(systimestamp, 'DD-Mon-YYYY "T"HH24:MI:SSTZH:TZM') from dual;
TO_CHAR(SYSTIMESTAMP,'DD-MON-YYYY"T"
------------------------------------
14-Feb-2018 T20:24:58+00:00
which happens to still show +00:00 because my system is in the UK. With a different value it shows something appropriate:
alter session set time_zone = 'AMERICA/NEW_YORK';
select to_char(current_timestamp, 'DD-Mon-YYYY "T"HH24:MI:SSTZH:TZM') from dual;
TO_CHAR(CURRENT_TIMESTAMP,'DD-MON-YY
------------------------------------
14-Feb-2018 T15:28:57-05:00
Notice now I'm using systimestamp and current_timestamp, which are TZ-aware, and not sysdate or current_date which are not - you'l get an error if you try to get the TZH or TZM elements from those.
The format you are requesting doesn't make much sense. +00:00 is the time zone offset (otherwise what is it?) but in Oracle the DATE data type does not know about time zones. Only the Oracle data type TIMESTAMP WITH TIME ZONE should be formatted that way in Oracle.
Here is how this should be done with timestamps WITH TIME ZONE. Note that the standard SYSTIMESTAMP function is a timestamp WITH TIME ZONE. In the query below, you can see how the timestamp is formatted using my session's default, and then using an explicit format model.
SQL> select systimestamp,
2 to_char(systimestamp, 'dd-Mon-yyyy "T"hh24:mi:sstzh:tzm') as ts
3 from dual
4 ;
SYSTIMESTAMP TS
------------------------------------------- ---------------------------
14-FEB-18 12.14.18.537000 PM -08:00 14-Feb-2018 T12:14:18-08:00

oracle: how to get the local timestamp insteadof the server timestamp

Suppose I have a table foo with the colum changed_colum of type date.
Now i would like to get the local timestamp from that colum, I try the following:
select TO_CHAR(cast (changed_colum as timestamp) at local, 'YYYY-MM-DD HH24:MI:SS TZR') from foo
result--->
2016-07-08 08:48:35 EUROPE/BERLIN
but this print the date of the application server. I need the local date of the session and not of the server.
Any Idea?
According to Oracle's documents at:
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions037.htm
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions079.htm#i999873
try this:
SELECT SESSIONTIMEZONE, CURRENT_TIMESTAMP FROM DUAL;
or
this:
SELECT CURRENT_TIMESTAMP, LOCALTIMESTAMP FROM DUAL;
Hope this will help.
Not clear what you are asking... If you know the dates are all "using the server time zone", then you can use
from_tz(cast (changed_column as timestamp), dbtimezone) at time zone sessiontimezone
and then format it as needed.
DATE datatype does not contain any time zone information. What is the time zone of these dates? Unless you don't know this from design of your application you cannot convert.
AT LOCAL is equal to AT TIME ZONE SESSIONTIMEZONE, i.e. the timezone set by your application server at logon (or set by ALTER SESSION SET TIME ZONE = ...). If you like this time zone equal to the timezone of your client then you have to code this at the application server.

Different CURRENT_TIMESTAMP and SYSDATE in oracle

After executing this SQL in oracle 10g:
SELECT SYSDATE, CURRENT_TIMESTAMP FROM DUAL
I receive this strange output:
What is cause of the difference in time?
The server time is equal of SYSDATE value
CURRENT_DATE and CURRENT_TIMESTAMP return the current date and time in the session time zone.
SYSDATE and SYSTIMESTAMP return the system date and time - that is, of the system on which the database resides.
If your client session isn't in the same timezone as the server the database is on (or says it isn't anyway, via your NLS settings), mixing the SYS* and CURRENT_* functions will return different values. They are all correct, they just represent different things. It looks like your server is (or thinks it is) in a +4:00 timezone, while your client session is in a +4:30 timezone.
You might also see small differences in the time if the clocks aren't synchronised, which doesn't seem to be an issue here.
SYSDATE, SYSTIMESTAMP returns the Database's date and timestamp, whereas current_date, current_timestamp returns the date and timestamp of the location from where you work.
For eg. working from India, I access a database located in Paris. at 4:00PM IST:
select sysdate,systimestamp from dual;
This returns me the date and Time of Paris:
RESULT
12-MAY-14 12-MAY-14 12.30.03.283502000 PM +02:00
select current_date,current_timestamp from dual;
This returns me the date and Time of India:
RESULT
12-MAY-14 12-MAY-14 04.00.03.283520000 PM ASIA/CALCUTTA
Please note the 3:30 time difference.
SYSDATE returns the system date, of the system on which the database resides
CURRENT_TIMESTAMP returns the current date and time in the session time zone, in a value of datatype TIMESTAMP WITH TIME ZONE
execute this comman
ALTER SESSION SET TIME_ZONE = '+3:0';
and it will provide you the same result.
SYSDATE provides date and time of a server.
CURRENT_DATE provides date and time of client.(i.e., your system)
CURRENT_TIMESTAMP provides data and timestamp of a clinet.
Note: SYSDATE - returns only the date, i.e., "yyyy-mm-dd" is not correct. SYSDATE returns the system date of the database server including hours, minutes, and seconds. For example:
SELECT SYSDATE FROM DUAL;
will return output similar to the following: 12/15/2017 12:42:39 PM
SYSDATE, systimestamp return datetime of server where database is installed. SYSDATE - returns only date, i.e., "yyyy-mm-dd". systimestamp returns date with time and zone, i.e., "yyyy-mm-dd hh:mm:ss:ms timezone"
now() returns datetime at the time statement execution, i.e., "yyyy-mm-dd hh:mm:ss"
CURRENT_DATE - "yyyy-mm-dd", CURRENT_TIME - "hh:mm:ss", CURRENT_TIMESTAMP - "yyyy-mm-dd hh:mm:ss timezone". These are related to a record insertion time.