Oracle (SQL Developer): How to convert TIMESTAMP string includeing UTC Offset - sql

Objective
Identify the correct TIMESTAMP format and the cause of the issue.
Problem
Tring to load a CSV which includes timestamp including UTC offset.
2014-01-01T00:38:51.000+11:00
The format string is below.
YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM
However, getting an error message.
Invalid format YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM is specified.
SQL Developer
TIMESTAMP
TIMESTAMP with TZ
Please suggest how to fix this and the reason.
References
Oracle 9: Convert date from mm/dd/yyyy hh:mm:ss format to iso8601 formatted datetime [closed]

There is nothing wrong with the timestamp with timezone format:
SQL> select to_timestamp_tz('2014-01-01T00:38:51.000+11:00',
2 'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM') as result from dual;
RESULT
---------------------------------------------------------------------------
01-JAN-14 12.38.51.000000000 AM +11:00
1 row selected.
Elapsed: 00:00:00.00
(What is DISPLAYED is in a different format - it uses my NLS_TIMESTAMP_TZ_FORMAT session parameter - but the conversion from string to timestamp with timezone worked perfectly fine.)
Definitely a SQL Developer issue - you will need to find out how this is done in their interface.

Related

correct to_char date syntax to have trailing zeroes after milliseconds

My current query in oracle sql for getting a timestamp format is TO_CHAR(c2.start_on,'DD-MM-YY HH:MI:SS.FF PM'), it outputs the timestamp like this 25-11-20 07:00:13.36 PM
However I want it to display the date in this way 25-11-20 07:00:13.360000000 PM
What should I add in the timestamp format for this to be possible ?
I have tried doing it like this HH:MI:SS.FM00000 as suggested here
but it gives me the error. ORA-01821: date format not recognized
what is the correct way to get the date in the desired format ?
If you want fractional seconds, you don't want a DATE, you want a TIMESTAMP. So here's a timestamp formatted with 6 digits of precision
select to_char(systimestamp, 'HH:MI:SS.FF6') from dual;
If you have a date, you could convert it to a TIMESTAMP (using CAST AS TIMESTAMP), but better to look at updating your data model to use the proper type for the source column as starters.

Timestamp string conversion / from_utc_timestamp

I need to convert 2021-10-03 15:10:00.0 as 2021-10-03T15:10:00-04:00
I tried with.
from_utc_timestamp(from_unixtime(unix_timestamp('2021-10-03 15:10:00.0', "yyyy-MM-dd HH:mm:ss.S"),"yyyy-MM-dd'T'HH:mm:ssXXX"),"America/New_York")
I got Null value
Any suggestions please
from_utc_timestamp can accept timestamp or compatible string (yyyy-MM-dd HH:mm:ss.S), or bigint, not this: "yyyy-MM-dd'T'HH:mm:ssXXX"
Hive timestamps are timezoneless. Once you converted from UTC to America/NY, the timezone information is lost, only you know in which timezone it is, having timestamp converted it is already impossible to derive the timezone from it.
You can concatenate with timezone, conversion like this returns what you need but it works for particular date only. In December -05:00 timezone should be usedm not +04:00:
date_format(from_utc_timestamp('2021-10-03 15:10:00.0',"America/New_York"),"yyyy-MM-dd'T'HH:mm:ss+04:00") --This is wrong!!!
From_utc_timestamp is Daylight saving aware. It can be -05:00 or -04:00 depending on the date.
Consider this example, first returns 5, second returns 4:
select (unix_timestamp("2020-01-01 12:00:00.0")-unix_timestamp(from_utc_timestamp("2020-01-01 12:00:00.0","America/New_York")))/60/60
select (unix_timestamp("2020-10-19 12:00:00.0")-unix_timestamp(from_utc_timestamp("2020-10-19 12:00:00.0","America/New_York")))/60/60
So, you can get current time zone corresponding to America/New_York for the same timestamp and concatenate it with converted timestamp:
select concat(date_format(from_utc_timestamp('2021-10-03 15:10:00.0',"America/New_York"),"yyyy-MM-dd'T'HH:mm:ss"),'+0',
--get hrs shift
(unix_timestamp("2021-10-03 15:10:00.0")-unix_timestamp(from_utc_timestamp("2021-10-03 15:10:00.0","America/New_York"))) div 3600,':00')
Result:
2021-10-03T11:10:00+04:00
It should work correctly with different timestamps taking into account daylight saving time for America/New_York.

Converting string to timetsamp in Hive

I have a pipeline where im getting data from sqlserver and load it into Hive table.I have a timestamp column in the source which is like 'YYYY-MM-DD HH:MM:SS'
Sql table(datetime) ---> Hive stage table(string)---->Hive final table(timestamp)
The source table is in US/Pacific time zone. In the middle stage table, the format is like 'YYYY-MM-DD HH:MM:SS.0'.
How do i convert into a timestamp field for the final table? I want the final table column to look like 'YYYY-MM-DD HH:MM:SS'
I see from_unixtime being used, but when i try like below,it returns null.
FROM_UNIXTIME(UNIX_TIMESTAMP('date column','yyyy-mm-dd HH.mm.ss')) as ts
Im pretty new to using Hive and need some suggestion on what should i do here, Thanks.
If the timestamp string is in format 'yyyy-MM-dd HH:mm:ss.S' then you can cast it to timestamp type using timestamp() function.
timestamp(col)
Also you can insert string directly into timestamp column.
It works because 'yyyy-MM-dd HH:mm:ss.S' - is a default timestamp format.
You need conversion using FROM_UNIXTIME(UNIX_TIMESTAMP(col, format)) if the format is not 'yyyy-MM-dd HH:mm:ss.S'. This format you should convert to, not from. Specify correct FROM format, it is case-sensitive: MM is not the same as mm, delimiters do matter: dot is not the same as semicolon or space, etc.
See format manual here: SimpleDateFormat
Also see this post about timestamp with nanoseconds

H2 Database Delete Records Older than particular Days

I have to delete all the records from H2 Database by matching completed_date column with current time stamp where difference in days are greater than 1 (or an number of days).
There is one problem with the schema of database that completed_date is stored as String in the given format 11-Jan-2018 15:35:30 PM i.e 'dd-MMM-yyyy HH:mm:ss aaa' format.
There are some more parameters where status should be matched.
I am getting this Exception
Cannot parse "TIMESTAMP" constant "28-12-2017 03:12:47"; SQL statement:
The query I have written is as below.
delete from TABLE_NAME
where
status = 'status1'
OR status = 'status2'
OR status = 'status3'
AND
TIMESTAMPDIFF(DAY,TO_CHAR(PARSEDATETIME(completed_date,'dd-MMM-yyyy HH:mm:ss
aaa'),'dd-MM-yyyy HH:mm:ss'),CURRENT_TIMESTAMP()) >= 1;
In above query I have found that TIMESTAMPDIFF does not work with date format dd-MMM-yyyy HH:mm:ss aaa so first I have tried to parse it in 'dd-MM-yyyy HH:mm:ss' format and if use this below query it gives me proper result
SELECT TO_CHAR(PARSEDATETIME('2017-OCT-2017 15:49:47 PM','dd-MMM-yyyy
HH:mm:ss aaa')
,'dd-MM-yyyy HH:mm:ss')
Just giving some more information that i am coding Workfusion which is RPA tool which internally uses h2-database so if anyone from workfusion here they can also help me.
timestampdiff() takes two timestamps as the input, however you are passing a string and a timestamp. So the string gets converted back to a timestamp using some default format.
You should be using:
TIMESTAMPDIFF(DAY,PARSEDATETIME(completed_date,'dd-MMM-yyyy HH:mm:ss aaa'), CURRENT_TIMESTAMP())
(assuming that parsedatetime() can successfully parse the string)

Oracle Date Conversion from a sybase table

I am trying to use Oracle SQL Developer to import a CSV file to a table. One of the fields is in day/time format. An example of such a date is '9/15/1993 12:00:00.000 AM'. IN SQL Developer when it asks me what date format to use I enter MM/DD/YYYY HH:MI:SS AM but this creates a ORA-01855 error complaining about AM so I imagine something is wrong. Any ideas?
It's probably more likely to be the milliseconds rather than the AM/PM indicator. Convert to a timestamp first then cast to date: String to date in Oracle with milliseconds