Join on time part is failing - teradata

I am facing an issue where i am not able to join two time columns:
The reported date is timestamp 0 with values like
4/1/2017 19:58:00
8/19/2017 19:58:00
Fault_Order_Submitted_Dt DATE FORMAT 'YYYY-MM-DD',
Fault_Order_Submitted_Tm INTEGER FORMAT '99:99:99',
sel a.completion_date, b.Fault_Order_Completed_Dt||b.fault_order_submitted_tm as Fault_time from
DG.LL_FMFTMX a
inner join DG.fault_order b
on a.fault_number=b.Fault_Order_Num
and cast(a.reported_date as date) =b.fault_order_submitted_dt
and cast(cast( a.reported_date as time(0) ) as integer format '99:99:99') = b.fault_order_submitted_tm
where fault_status='P'
The Join part on time is failing with invalid operation on date/time.
Please suggest.

To get the time portion as HHMISS integer you need to cast it to a string first:
Cast(To_Char(reported_date, 'hhmiss') AS INT)

Related

How to return the previous 7 days in Teradata

SELECT
DD.DATE_DATE AS Claim_Rcv_Date
from claim claim INNER JOIN DIM_DATE DD
ON DD.DATE_DIM_CK = CLAIM.CLAIM_RCVD_DATE_DIM_CK
WHERE ???
How would I limit to the previous 7 days? Using the DD.DATE_DATE AS Claim_Rcv_Date
Assuming the date column is actually stored as a proper date type, you can use:
WHERE your_date_column BETWEEN (CURRENT_DATE - INTERVAL '7' DAY) AND CURRENT_DATE

Hive: Convert string datetime with missing seconds in "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

I'm using the following code to convert a string datetime variable to datetime, but the converted string is missing SSS part.
Code used:
cast(FROM_UNIXTIME(UNIX_TIMESTAMP(oldtime, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"),"yyyy-MM-dd HH:mm:ss.SSS") as timestamp) as newtime
The outcome:
2019-03-08T18:28:36.901Z is converted to 08MAR2019:18:28:36.000000
Some other oldtimes in string:
2020-03-09T16:05:06:827Z
2020-03-09T16:03:19:354Z
2020-03-11T16:03:57:280Z
2020-03-10T16:02:57:642Z
2020-03-10T16:04:07:455Z
2020-03-10T16:04:09:737Z
2020-03-10T16:03:57:280Z
2020-03-10T16:02:46:816Z
The SSS part '901' is missing in the converted time. Would like help on keeping the SSS part since I need to sort the records by their exact time.
Thank you!
from_unixtime is always until minutes(yyyy-MM-dd HH:mm:ss) to get millisecs we need to do some workarounds.
we will extract the millisecs from the old_time using regexp_extract then concat that to from_unixtime result and finally cast to timestamp.
Example:
select old_time,
timestamp(concat_ws(".", --concat_ws with . and cast
FROM_UNIXTIME(UNIX_TIMESTAMP(old_time, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"),"yyyy-MM-dd HH:mm:ss"), -- from_unixtime and unix_timestamp to convert without millisecs
regexp_extract(string(old_time),".+\\.(.*)(?i)z",1))) as newtime from --regexp_extract to extract last 3 digits before z then concat
(select string("2020-03-11T21:14:41.335Z")old_time)e
+------------------------+-----------------------+
|old_time |newtime |
+------------------------+-----------------------+
|2020-03-11T21:14:41.335Z|2020-03-11 21:14:41.335|
+------------------------+-----------------------+
UPDATE:
Your sample data have : before milliseconds, Try with below query:
select old_time,
timestamp(concat_ws(".", --concat_ws with . and cast
FROM_UNIXTIME(UNIX_TIMESTAMP(old_time, "yyyy-MM-dd'T'HH:mm:ss:SSS'Z'"),"yyyy-MM-dd HH:mm:ss"), -- from_unixtime and unix_timestamp to convert without millisecs
regexp_extract(string(old_time),".+\\:(.*)(?i)z",1))) as newtime from --regexp_extract to extract last 3 digits before z then concat
(select string("2020-03-11T21:14:41:335Z")old_time)e
Simply replace 'T' with space ' ' remove 'Z' and replace last ':' with dot, like this :
select regexp_replace('2020-03-09T16:05:06:827Z','(.*?)T(.*?):([^:]*?)Z$','$1 $2\\.$3');
Result:
2020-03-09 16:05:06.827
Read also this answer if you need to convert to different format, preserving milliseconds: https://stackoverflow.com/a/59645846/2700344

Convert string to date with date and time together in Hive

I need convert string to datetime (date and time together).
I try this:
cast(to_date(from_unixtime(unix_timestamp('20190303164305', 'yyyyMMddHHmmss'))) as date) as date_data_chamada
timezone: Brazil
But this way returns just date, like this: 2019-03-03, and I need: 2019-03-03 16:43:05
Thanks!
Full code:
INSERT INTO p_b.este PARTITION (dt_originacao_fcdr)
SELECT
tp_registro_fcdr,
seq_registro_fcdr,
tp_cdr_fcdr,
dt_atendimento_fcdr,
data_atendimento_completa_fcdr,
cast(from_unixtime(unix_timestamp(data_atendimento_completa_fcdr, 'yyyyMMddHHmmss'),"yyyy-MM-dd HH:mm:ss")as timestamp) as date_data_atendimento_fcdr,
hr_atendimento_fcdr,
duracao_atend_fcdr,
hr_originacao_fcdr,
duracao_total_fcdr,
duracao_chamada_tarifada_fcdr,
st_chamada_fcdr,
fim_sel_orig_fcdr
FROM p_b.norm;
Remove date casting and to_date functions as you are expecting timestamp!
Example:
hive> select from_unixtime(unix_timestamp('20190303164305', 'yyyyMMddHHmmss'),"yyyy-MM-dd HH:mm:ss") as date_data_chamada;
RESULT:
2019-03-03 16:43:05
If you use to_date or cast('string' as date) then hive results only date(yyyy-MM-dd)!
Ex:
hive> select to_date(from_unixtime(unix_timestamp('20190303164305', 'yyyyMMddHHmmss'),"yyyy-MM-dd HH:mm:ss")) as date_data_chamada;
--2019-03-03
Pass the second argument format string to from_unixtime. Note that the returned type is string.
from_unixtime(unix_timestamp('20190303164305','yyyyMMddHHmmss'),'yyyy-MM-dd HH:mm:ss')

How to proper cast in sqllite

When i cast datetime in SQLLite, it truncates the string.
for example
select cast("2017-04-23 9:12:08 PM" as datetime) as dt
returns
2017
SQLite's CAST can only cast to the defined storage classes and can therefore only be used to cast to
NONE (blob), TEXT, REAL, INTEGER or NUMERIC.
However the normal rules for determing column-affinity are applied to the type so by coding CAST(value AS datetime) you are effectively using CAST(value AS NONE) (i.e. a BLOB).
CAST expressions
Therefore you can't effectively use CAST. However you simply use the DateTime functions against an appropriate value (accepted formats) as per Date And Time Functions e.g. :-
SELECT datetime("2017-04-23 09:12:08") as dt;
results in
2017-04-23 09:12:08
or to show date manipulation
select date(dt), dt FROM (
select datetime("2017-04-23 09:12:08") as dt
);
results in
2017-04-23
and
2017-04-23 09:12:08
However considering that your format isn't one of the accepted formats you could convert the value. This is more complex but it can be done. Here's an example that will perform the conversion (not substantially tested though) :-
SELECT
CASE WHEN (CAST(hour AS INTEGER) + CAST(adjustment AS INTEGER)) > 9 THEN
datepart||' '||CAST(CAST(hour AS INTEGER) + CAST(adjustment AS INTEGER) AS TEXT)||':'||mins_and_secs
ELSE
datepart||' 0'||CAST(CAST(hour AS INTEGER) + CAST(adjustment AS INTEGER) AS TEXT)||':'||mins_and_secs
END AS converted
FROM (
SELECT substr(ts,1,10) as datepart,
CASE WHEN instr(ts,"PM") THEN 12 ELSE 0 END AS adjustment,
CASE WHEN length(ts) = 21 THEN substr(ts,12,1) ELSE substr(ts,12,2) END AS hour,
CASE WHEN length(ts) = 21 THEN substr(ts,14,5) ELSE substr(ts,15,5) END AS mins_and_secs
FROM (
select("2017-04-23 9:12:08 PM") as ts
)
);
This would result in 2017-04-23 21:12:08.
Using select("2017-04-23 9:12:08 AM") results in 2017-04-23 09:12:08
Using select("2017-04-23 11:12:08 PM") results in 2017-04-23 23:12:08
Using select("2017-04-23 11:12:08 AM") results in 2017-04-23 11:12:08
The closest I could come up with is:
select date(datetime(strftime('%s','2017-04-23 09:12:08'), 'unixepoch'))
Result:
2017-04-23
The dateformat you have is not recognised by SQLite:
"2017-04-23 9:12:08 PM"
It does not conform to the Time string formats recognised:
A time string can be in any of the following formats:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
Date And Time Functions

Convert varchar(50) to Date Format 'yyyy-mm-dd' in teradata?

I have a column defined as Date format yyyy-mm-dd while creating.
I want to insert data from other table which has that column as varchar(50)
While selecting and inserting into the table I got this error
INSERT Failed. 2665: Invalid date.
Can someone help me in casting this?
INSERT INTO TEMP_TABLES.FACT
(
CUSTOMER_ACCOUNT_ID,
LOB_START_DATE,
)
SEL
CUSTOMER_ACCOUNT_ID,
I.start_date as LOB_START_DATE,
FROM #LOGIN I
left join JOURNEY_TABLE.DOTCOM_DIM d1
on I.PAGES = d1.PAGE_DESC
This is the example of date stored in varchar(50) field : 2014-04-03
Thanks in advance
In case it would be helpful, here's a query that should allow you to identify the rows with invalid dates:
select
*
from
#login t1
left outer join sys_calendar.calendar t2
on t1.start_date = cast (cast(t2.calendar_date as date format 'YYYY-MM-DD') as char(10))
where t2.calendar_date is null
Any rows that return from this query will have invalid dates.
I think all you need is the format statement
examples here
SEL
CUSTOMER_ACCOUNT_ID,
cast(((I.start_date (date, format 'yyyy-mm-dd'))(char(10))) as LOB_START_DATE,
In more recent versions of Teradata there is a TRYCAST() function. This function will attempt to cast the data and return NULL if the conversion fails, instead of failing the statement.
INSERT INTO TEMP_TABLES.FACT
(
CUSTOMER_ACCOUNT_ID,
LOB_START_DATE,
)
SELECT Customer_Account_ID
, TRYCAST(I.Start_Date AS DATE) AS LOB_START_DATE
FROM #LOGIN I
LEFT JOIN JOURNEY_TABLE.DOTCOM_DIM d1
ON I.Pages = d1.Page_Desc;

Resources