Oracle - Convert and display sysdate in EST - oracle11g

Using oracle 11 , how to Convert and display sysdate in EST?
Server is in PST location
I tried this:
select from_tz(CAST(sysdate AS TIMESTAMP),'EST') from dual
/
I don't get correct results..

Use systimestamp since that includes a time zone.
select systimestamp at time zone 'US/Eastern'
from dual;
should return a timestamp in the Eastern time zone (assuming your database time zone files are up to date).
Note that if you ask for a timestamp in EST, that should be an hour earlier than the current time in the Eastern time zone of the United States because the US is in Daylight Savings Time. So the Eastern time zone is in EDT currently not EST.

Use systimestamp rather than sysdate, because it is already timezone-aware; then at time zone to translate:
select systimestamp at time zone 'America/New_York' from dual
Using a region is better/safer than an abbreviation like 'EST", which might not be unique; and gives you the wrong answer - this shows mine, Justin's alternative (and maybe better!) region, what you get with EST, and all the things EST is an abbreviation of. (You could use EST5EDT, but a region is still clearer.)
You could also set your session to an East-coat region (if it isn't already) and then use current_timestamp instead of systimestamp:
alter session set time_zone = 'America/New_York';
select current_timestamp from dual;
db<>fiddle
Either way, how it is displayed is down to your client/application - e.g. the session nls_timestamp_tz_format - unless you convert it to a string with to_char(), supplying the format you want.

If you really want to use SYSDATE then you can use:
SELECT from_tz(CAST(sysdate AS TIMESTAMP),'PST') AT TIME ZONE 'EST5EDT' FROM DUAL;
Which outputs:
| FROM_TZ(CAST(SYSDATEASTIMESTAMP),'PST')ATTIMEZONE'EST5EDT' |
| :--------------------------------------------------------- |
| 02-APR-21 11.07.33.000000 PM EST5EDT |
or:
SELECT from_tz(CAST(sysdate AS TIMESTAMP),'US/Pacific') AT TIME ZONE 'US/Eastern' FROM DUAL;
Which outputs:
| FROM_TZ(CAST(SYSDATEASTIMESTAMP),'US/PACIFIC')ATTIMEZONE'US/EASTERN' |
| :------------------------------------------------------------------- |
| 02-APR-21 11.07.33.000000 PM US/EASTERN |
db<>fiddle here

Related

How can I convert 12 hour format hours into 24 hour format time in ORACLE SQL like 01-FEB-18 01.00.21.645000000 PM should be 05-01-0018 13:12:44

there are multiple timestamps column in oracle Sql , Now in want to convert 12 hour format time into 24 hour format time like 01-FEB-18 01.00.21.645000000 PM should be 05-01-0018 13:12:44. but I need to convert hours into 24 hours time format , I am using the below statement.
SELECT TO_CHAR(TO_DATE(PERFORMED_TIMESTAMP,'DD-MON-YYYY hh:mi:ss AM'),'DD-MM-YYYY hh24:mi:ss')
FROM FACT_WORK_ITEM_ACTION
WHERE TRUNC(PERFORMED_TIMESTAMP)>= '05-JUN-18'
But still i am getting the below error .
Error Code....
ORA-01855: AM/A.M. or PM/P.M. required
01855. 00000 - "AM/A.M. or PM/P.M. required"
*Cause:
*Action:
SELECT to_char( TO_timestamp('01-FEB-18 01.21.01.645','dd-mon-yy hh12.mi.ss.ff'),'dd-mm-yyyy hh24:mi:ss')
from dual;
Your PERFORMED_TIMESTAMP is already a timestamp, which doesn't have any intrinsic human-readable format - Oracle uses its own internal representation when storing the values.
You are just seeing that timestamp displayed by your client with formatting taken from the session NLS settings (or, possibly, overridden by the client itself).
The error is because you are doing unnecessary data type conversions and relying on implicit conversion and NLS session settings. When you do
TO_DATE(PERFORMED_TIMESTAMP,'DD-MON-YYYY hh:mi:ss AM')
you are first implicitly converting the PERFORMED_TIMESTAMP to a string, again using your session NLS settings, so you're effectively doing:
TO_DATE(TO_CHAR(PERFORMED_TIMESTAMP),'DD-MON-YYYY hh:mi:ss AM')
which given the string values in your question is actually:
TO_DATE(TO_CHAR(PERFORMED_TIMESTAMP, 'DD-MON-RR HH.MI.SS.FF AM'),'DD-MON-YYYY hh:mi:ss AM')
The inner part of that will give a string like "01-FEB-18 01.00.21.645000000 PM" which is what you see when you query the table and the client does its own formatting. Passing that string back into to_date() gives the error you see, because the fractional seconds are appearing in the string where it's expecting to see the AM/PM marker:
SELECT TO_DATE('01-FEB-18 01.00.21.645000000 PM','DD-MON-YYYY hh:mi:ss AM')
FROM DUAL;
Error report -
ORA-01855: AM/A.M. or PM/P.M. required
You could replace the implcit conversion and session values with explicit conversion and format masks:
TO_CHAR(TO_DATE(TO_CHAR(PERFORMED_TIMESTAMP, 'DD-MON-YYYY hh:mi:ss AM'),'DD-MON-YYYY hh:mi:ss AM'), 'DD-MM-YYYY hh24:mi:ss')
But hopefully you can tell that is doing a lot more work than it needs to -and having to use the same format mask twice is also a sign that you're doing something wrong.
The real solution is just to simplify it. You don't need to convert to a string and back to a date at all. Just do:
SELECT TO_CHAR(PERFORMED_TIMESTAMP, 'DD-MM-YYYY HH24:MI:SS')
FROM FACT_WORK_ITEM_ACTION
WHERE PERFORMED_TIMESTAMP >= TIMESTAMP '2018-06-05 00:00:00';
Notice that I've also removed the trunc() and the comparison with a string; this now compares your timestamp column as a timestamp, which will make any index on that happier too.
Quick demo with a CTE to provide dummy data:
with FACT_WORK_ITEM_ACTION(PERFORMED_TIMESTAMP) as (
select timestamp '2018-06-01 13:00:21.645000000' from dual
union all select timestamp '2018-06-06 13:00:21.645000000' from dual
)
SELECT TO_CHAR(PERFORMED_TIMESTAMP, 'DD-MM-YYYY HH24:MI:SS')
FROM FACT_WORK_ITEM_ACTION
WHERE PERFORMED_TIMESTAMP >= TIMESTAMP '2018-06-05 00:00:00';
TO_CHAR(PERFORMED_T
-------------------
06-06-2018 13:00:21

Format UTC Time To Local Time

When you have a string that is formatted in UTC Time, how can I format it to a DateTime but local time?
For example, if I have the below code, it improperly formats my code (meaning incorrect time)
string dateformatted = "2017-01-10T11:13:00-07:00"
DateTime.Parse(Convert.ToString(dateformatted));
However, the output from this is
01/10/2017 1:13:00 PM
Which is 2 hours ahead of the actual time of 11:13:00. How can I convert the string to the proper timezone time?
I believe you are looking for the Parse(String, IFormatProvider, DateTimeStyles) overload of the Parse method. The third parameter, DateTimeStyles, will allow for force or prohibit conversion between local and UTC times. Options include: AdjustToUniversal, AssumeLocal, and AssumeUniversal (among others).
According MSDN docs:
Return value Type: DateTime
An object that is equivalent to the date and time contained in s.
Generally, the Parse method returns a DateTime object whose Kind
property is DateTimeKind.Unspecified. However, the Parse method
may also perform time zone conversion and set the value of the Kind
property differently,depending on the values of the s and styles
parameters:
If:
+-------------------------------------------------------------------------------------------+---------------------------------------------------------------------+--------------------+
| If | Time zone conversion | Kind property |
+-------------------------------------------------------------------------------------------+---------------------------------------------------------------------+--------------------+
| s contains time zone information. | The date and time is converted to the time in the local time zone. | DateTimeKind.Local |
+-------------------------------------------------------------------------------------------+---------------------------------------------------------------------+--------------------+
| s contains time zone information, and styles includes the AdjustToUniversalflag. | The date and time is converted to Coordinated Universal Time (UTC). | DateTimeKind.Utc |
+-------------------------------------------------------------------------------------------+---------------------------------------------------------------------+--------------------+
| s contains the Z or GMT time zone designator, and styles includes the RoundtripKind flag. | The date and time are interpreted as UTC. | DateTimeKind.Utc |
+-------------------------------------------------------------------------------------------+---------------------------------------------------------------------+--------------------+
You should add timezone.

What format is the Safari History.db history_visits.visit_time in?

When looking at the History.db from Safari, there's a table named history_visits which has a column named visit_time, which is a REAL value. It has values such as 470799793.096987. What format is that in? I'd like to see it in a format such as 12/08/2015 05:12:05.
It's the number in seconds since 00:00:00 UTC on 1 January 2001. It must be coming from an NSDate.
NSDate objects encapsulate a single point in time, independent of any particular calendrical system or time zone. Date objects are immutable, representing an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001).
— NSDate Class Reference
To get a decent human value out of it, you must add 978307200 (the epoch for 2001-01-01 00:00:00).
This query should give you what you want:
.headers on
select datetime(v.visit_time + 978307200, 'unixepoch', 'localtime') as date, v.visit_time + 978307200 as epoch, v.visit_time, i.domain_expansion, i.url
from history_items i left join history_visits v on i.id = v.history_item
order by i.id desc
limit 100;
Example output:
date|epoch|visit_time|domain_expansion|url
2015-12-31 11:51:27|1451562687.28465|473255487.284646|duckduckgo|https://duckduckgo.com/?q=current+timestamp+2015-12-31+11:51&t=osx
PS: Just for future reference, the Safari db file is located at ~/Library/Safari/History.db
To convert the visit_time value in the history.db in an excel spread sheet, open the history.db file in a tool such as DB browser for SQLLite (Windows) and export the history_visits values to a CSV file.
Open the CSV file and create a column where you will populate your values in human readable time adjusted to your time zone, and use the following formula convert your NSDate:
=((((C2+978307200)/60)/60)/24)+DATE(1970,1,1)+(-5/24)
In the above formula, the time value is in cell C2, and my time zone GMT-5. To adjust to your own time zone adjust the statement in the last set of parenthesis. Presently I have (-5/24) to represent GMT-5.
When I first approached this conversion, I mistakenly assumed the time in the history.db to be epoch time, which starts at 1/1/1970, and did not understand why there was such a skew in time. Adding the required conversion factor +978307200 solved the problem.
I found the domain_expansion field to be null in some cases, here's a modified query:
SELECT SUBSTR(
SUBSTR(url, INSTR(url, '/')+2),
1,
INSTR(SUBSTR(url, INSTR(url, '/')+2),'/') - 1
) domain,
url,
datetime(hv.visit_time + 978307200, 'unixepoch', 'localtime') visit_time
FROM history_items hi
JOIN history_visits hv on hi.id = hv.history_item;

SQLite timestamp conversion function

I've inherited a SQLite DB, in it I've a TIMESTAMP field called ZDATE.
One value is 401,580,000 and I know it correspond to Sept 23rd, 2013.
I've calculated that the 0 of this field is the midnight of Jan 1st, 2001 (?).
However, I didn't find any conversion function to get a formatted date, in fact if I use date() I get:
ZDATE date(zdate)
401580000 1094776-12632211-20
Any help will be appreciated.
> select 401580000 / (julianday('2013-09-23') - julianday('2001-01-01'));
86398.4509466437
> select 60*60*24;
86400
So this timestamp appears to use seconds.
To convert it into a timestamp that SQLite can use directly, i.e., a Unix epoch timestamp, just add the appropriate offset:
> select datetime(401580000 + strftime('%s', '2001-01-01 02:00:00'), 'unixepoch');
2013-09-23 00:00:00

oracle 10g convert date column with timestamp to dd-mon-yyyy format

Hi am trying to convert column defined as date to date with format DD-MON-YYYY but can't get it working
select
to_date(to_char
(DESIGN_COMPLETION_DATE,'DD-MON-YYYY'),'DD-MON-YYYY') as Assessment_Completion_Date
from nbi_dates
Also Tried
Select to_date(DESIGN_COMPLETION_DATE,'DD-MON-YYYY') as Assessment_Completion_Date
from nbi_dates
What works is, but I can't do any calculations on it as it is char
Select to_char(DESIGN_COMPLETION_DATE,'DD-MON-YYYY') as Assessment_Completion_Date
from nbi_dates
Thanks
If you have a DATE that includes a time portion but are only interested in the actual date part, you can use the trunc function:
select trunc(design_completion_date) as assessment_completion_date from nbi_dates
An example of the difference using sysdate; notice the time on the trunc'd version has been set to midnight:
SQL> alter session set nls_date_format = 'DD/MM/YYYY HH24:MI:SS';
Session altered.
SQL> select sysdate, trunc(sysdate) from dual;
SYSDATE TRUNC(SYSDATE)
------------------- -------------------
11/04/2013 15:14:31 11/04/2013 00:00:00
A DATE has no inherent format. DD-MON-YYYY is a format mask applied to display the date, or to convert it to a string representation, which is usually only necessary for display anyway. What you have as your third option is right for that purpose, but not if you want to do any further date calculations with the result.
Select to_date(to_char(DESIGN_COMPLETION_DATE,'DD-MON-YYYY'),'DD-MON-YYYY') as Assessment_Completion_Date
from nbi_dates
or simply (in case you want date object for calculations but not for rendereing)
Select DESIGN_COMPLETION_DATE as Assessment_Completion_Date
from nbi_dates

Resources