How to parse varchar to actual time value? - sql

I am trying to insert the data into the final table in snowflake from the staging table. When the command is run it give the error:
Can't parse '20211101132344205550' as timestamp with format 'YYYYMMDD HH24:MI:SS.FF'
My table definition and insert statement is here.
I used the same kind of method last time it worked. Thank you so much in advance.
CREATE OR REPLACE TABLE dw.tb_fidctp_order(
sysdate DATE,
record_id NUMBER(18,0) ,
timestamp TIMESTAMP_NTZ(9),
<trim_excess>
);
INSERT INTO dw.tb_fidctp_order(
sysdate,
record_id,
timestamp,
<trim_excess>
)
SELECT
TO_DATE(LEFT(timestamp, 8), 'YYYYMMDD')
,CAST(record_id AS NUMBER(18,0))
,TO_TIMESTAMP(LEFT(timestamp,24),'YYYYMMDD HH24:MI:SS.FF')
<trim_excess>
FROM stg.tb_fidctp_order_input;

In Snowflake you need to define what your format is. So, if all of your timestamps are formatted as a straight string like that, then you are defining it incorrectly. Try something more like this:
SELECT to_timestamp(left(EXPIRY_DATETIME,24),'YYYYMMDDHH24MISSFF');
The to_timestamp() function is defining how the input string is formatted, not how you want the timestamp to be formatted as an output.

So the error message is the critical point, your formating string for timestamps have space and : time formatting, which needs to be removed.
Below I have used the tru_to_timestamp function because it returns NULL istead of erroring with is helpful to show the problem is your formatting:
SELECT
'20211101132344205550' AS a
,try_to_timestamp(a, 'YYYYMMDD HH24:MI:SS.FF') as b
,try_to_timestamp(a, 'YYYYMMDDHH24MISSFF') as c;
gives:
A
B
C
20211101132344205550
2021-11-01 13:23:44.205
which shows your ,TO_TIMESTAMP(LEFT(timestamp,24),'YYYYMMDD HH24:MI:SS.FF')
should be:
,TO_TIMESTAMP(LEFT(timestamp,24),'YYYYMMDDHH24MISSFF')
and then you would have no problem.

Related

Null result when converting String to Date format in Databricks SQL

I have a String column from a temp view, were dates values are stored in format of '2020/01/01'.
I do need to convert that field to a date value to be stored into a Delta table in 'YYY-MM-DD' format.
I've been trying different kind of SQL functions but none of them seems to works, as a result, every function retrieves a null value:
select to_date(dateField, 'yyyy-MM-dd'),
CAST(dateField as date),
etc..
from TMP_table
Every function results in a null value, and there's no information about that into Databricks documentation.
As Usagi said above, select to_date(dateField, 'yyyy/MM/dd') AS date works perfectly.

BigQuery: String to Timestamp

I have a timestamp from the source that has been loaded to BQ as a string. I'd like to write a query in BigQuery that will return timestamp in the following format 2020-01-06 11:09:14.000-0600. Here is the current format of the string field: 2020-01-06T11:09:14.000-0600, 2018-10-01T15:45:59.000-0500, etc.
I have tried the following:
SELECT parse_timestamp ("%Y-%m-%dT%H:%M:%S.%E3S", start_timestamp, "America/Chicago"), FROM bqtable
The goal is to perform arithmetic on the timestamp fields.
Any feedback is appreciated. Thank you.
I think the %S and %E3S% are conflicting, as they both are parsing the seconds part of the string.
Try this:
with data as (
select '2020-01-06T11:09:14.000-0600' as ts_string union all select '2018-10-01T15:45:59.000-0500'
)
select ts_string, parse_timestamp ("%Y-%m-%dT%H:%M:%E3S%z", ts_string, "America/Chicago") as ts
from data

How to query a timezone data type in oracle using the java time format

I have the below timestamp with timezone data stored in a table
create table sample(id number,last_modified_dt timestamp with time zone);
select last_modified_dt from sample;
last_modified_dt
-----------------
19-06-17 09:36:28.100452000 AM +00:00
My front end node.js application is trying to query using the below format,
'2017-06-19T09:36:28Z' (java timestamp format)
select last_modified_dt from sample where last_modified_dt = '2017-06-19T09:36:28Z'
i tried using
select last_modified_dt from sample where last_modified_dt = TO_TIMESTAMP('2017-06-19T09:36:28Z','YYYY-MM-DD"T"HH24:MI:SS"Z"');
but it returns empty result, clearly i know something is missing, can you please help me out.
Not the best solution, but a very quick solution is
if you always get the time in that format
select last_modified_dt from sample
where TRUNC(last_modified_dt) =
TRUNC(to_date (regexp_replace ('2017-06-19T09:36:28Z',
'[a-zA-Z]',' '),
'rrrr-mm-dd hh:mi:ss')) -- TRUNC can be added if only date is important for matching and time is not important.
REGEXP_REPLACE will remove occurrences of any character from the timestamp which you get from Java.
a-ZA-Z will make sure, all English Alphabets are removed.
if db format is dd-mm-yyyy, only if date is being stored as characters
select last_modified_dt from sample
where TRUNC(last_modified_dt) =
TRUNC(to_date (regexp_replace ('2017-06-19T09:36:28Z',
'[a-zA-Z]',' '),
'rrrr-mm-dd hh:mi:ss'))

Oracle: One attribute with date, another with date and time?

I am taking an introductory course to databases so I am a complete beginner. In the database I am supposed to create I have two tables, each contain a DATE datatype.
In the first table, I want it to only display a date (DD-MM-YY) and in the second table display a date and time (DD-MM-YY HH24:MM).
How can I format each attribute to have these respective formats? I've looked around and tried the following command:
ALTER SESSION SET nls_date_format = 'YYYY-MM-DD HH24:MI:SS'
Which works nicely for the date and time field but leaves 00:00:00 for the date only field. Which I do not want, so I reverted it back to nls_date_format = 'DD-MM-YY'
As of right now the following:
INSERT INTO ITEM (ITEM_ENDDATEANDTIME)
VALUES ('13-AUG-13 23:56:00');
Gives me the error: date format picture ends before converting entire input string
Any ideas? Again, I'm a beginner a lot of this is new to me! Thanks!
What you want to do is always store your date values as dates. Data manipulation is infinitely easier when you have stored them in this format instead of in a text based format.
Then, you output them into a more human readable format through a query using syntax similar to this:
SELECT DateField,
TO_CHAR(DateField, 'YYYY-MM-DD HH24:MI:SS') AS Date1,
TO_CHAR(Datefield, 'DD-MM-YY') AS Date2
FROM MyTable
This takes the date data and outputs it as a formatted string. I hope this helps.

In Oracle, convert number(5,10) to date

When ececute the following SQL syntax in Oracle, always not success, please help.
40284.3878935185 represents '2010-04-16 09:18:34', with microsecond.
an epoch date of 01 January 1900 (like Excel).
create table temp1 (date1 number2(5,10));
insert into temp1(date1) values('40284.3878935185');
select to_date(date1, 'yyyy-mm-dd hh24:mi:ssxff') from temp1
Error report: SQL Error: ORA-01861: literal does not match format
string
01861. 00000 - "literal does not match format string"
*Cause: Literals in the input must be the same length as literals in
the format string (with the exception of leading whitespace). If the
"FX" modifier has been toggled on, the literal must match exactly,
with no extra whitespace.
*Action: Correct the format string to match the literal.
Thanks to Mark Bannister
Now the SQL syntax is:
select to_char(to_date('1899-12-30','yyyy-mm-dd') +
date1,'yyyy-mm-dd hh24:mi:ss') from temp1
but can't fetch the date format like 'yyyy-mm-dd hh24:mi:ss.ff'. Continue look for help.
Using an epoch date of 30 December 1899, try:
select to_date('1899-12-30','yyyy-mm-dd') + date1
Simple date addition doesn't work with timestamps, at least if you need to preserve the fractional seconds. When you do to_timestamp('1899-12-30','yyyy-mm-dd')+ date1 (in a comment on Mark's answer) the TIMESTAMP is implicitly converted to a DATE before the addition, to the overall answer is a DATE, and so doesn't have any fractional seconds; then you use to_char(..., '... .FF') it complains with ORA-01821.
You need to convert the number of days held by your date1 column into an interval. Fortunately Oracle provides a function to do exactly that, NUMTODSINTERVAL:
select to_timestamp('1899-12-30','YYYY-MM-DD')
+ numtodsinterval(date1, 'DAY') from temp3;
16-APR-10 09.18.33.999998400
You can then display that in your desired format, e.g. (using a CTE to provide your date1 value):
with temp3 as ( select 40284.3878935185 as date1 from dual)
select to_char(to_timestamp('1899-12-30','YYYY-MM-DD')
+ numtodsinterval(date1, 'DAY'), 'YYYY-MM-DD HH24:MI:SSXFF') from temp3;
2010-04-16 09:18:33.999998400
Or to restrict to thousandths of a second:
with temp3 as ( select 40284.3878935185 as date1 from dual)
select to_char(to_timestamp('1899-12-30','YYYY-MM-DD')+
+ numtodsinterval(date1, 'DAY'), 'YYYY-MM-DD HH24:MI:SS.FF3') from temp3;
2010-04-16 09:18:33.999
An epoch of 1899-12-30 sounds odd though, and doesn't correspond to Excel as you stated. It seems more likely that your expected result is wrong and it should be 2010-04-18, so I'd check your assumptions. Andrew also makes some good points, and you should be storing your value in the table in a TIMESTAMP column. If you receive data like this though, you still need something along these lines to convert it for storage at some point.
Don't know the epoch date exactly, but try something like:
select to_date('19700101','YYYYMMDD')+ :secs_since_epoch/86400 from dual;
Or, cast to timestamp like:
select cast(to_date('19700101', 'YYYYMMDD') + :secs_since_epoch/86400 as timestamp with local time zone) from dual;
I hope this doesn't come across too harshly, but you've got to totally rethink your approach here.
You're not keeping data types straight at all. Each line of your example misuses a data type.
TEMP1.DATE1 is not a date or a varchar2, but a NUMBER
you insert not the number 40284.3878935185, but the STRING >> '40284.3878935185' <<
your SELECT TO_DATE(...) uses the NUMBER Temp1.Date1 value, but treats it as a VARCHAR2 using the format block
I'm about 95% certain that you think Oracle transfers this data using simple block data copies. "Since each Oracle date is stored as a number anyway, why not just insert that number into the table?" Well, because when you're defining a column as a NUMBER you're telling Oracle "this is not a date." Oracle therefore does not manage it as a date.
Each of these type conversions is calculated by Oracle based on your current session variables. If you were in France, where the '.' is a thousands separator rather than a radix, the INSERT would completely fail.
All of these conversions with strings are modified by the locale in which Oracle thinks your running. Check dictionary view V$NLS_PARAMETERS.
This gets worse with date/time values. Date/time values can go all over the map - mostly because of time zone. What time zone is your database server in? What time zone does it think you're running from? And if that doesn't spin your head quite enough, check out what happens if you change Oracle's default calendar from Gregorian to Thai Buddha.
I strongly suggest you get rid of the numbers ENTIRELY.
To create date or date time values, use strings with completely invariant and unambiguous formats. Then assign, compare and calculate date values exclusively, e.g.:
GOODFMT constant VARCHAR2 = 'YYYY-MM-DD HH24:MI:SS.FFF ZZZ'
Good_Time DATE = TO_DATE ('2012-02-17 08:07:55.000 EST', GOODFMT);