Convert query Teradata to Hive - hive

i have the following query and it executes successfully in teradata.
CASE
WHEN Cast(STG_101_118_INVM.ACCT_OPEN_DT AS INTEGER) > 2956565 THEN Cast('2999-12-31'AS DATE)
WHEN Cast(STG_101_118_INVM.ACCT_OPEN_DT AS INTEGER) = 0 THEN Cast('1900-01-01' AS DATE)
ELSE Cast('1900-01-01' AS DATE) + Cast(STG_101_118_INVM.ACCT_OPEN_DT AS INTEGER) - 1 END
AS ACCT_OPEN_DT
i have the following query and it executes successfully in teradata.
but when run on hive it doesn't work and it shows a warning like this.
Error while compiling statement: FAILED: SemanticException line
0:undefined:-1 Wrong arguments 'ACCT_OPEN_DT': No matching method for
class org.apache.hadoop.hive.ql.udf.generic.GenericUDFOPDTIPlus with
(date, int)
what should i change?
can you help me to convert this query to hive format?

You have to use date_add() to add ACCT_OPEN_DT INT to a date. You can use below SQL -
CASE
WHEN Cast(STG_101_118_INVM.ACCT_OPEN_DT AS INTEGER) > 2956565 THEN Cast('2999-12-31'AS DATE)
WHEN Cast(STG_101_118_INVM.ACCT_OPEN_DT AS INTEGER) = 0 THEN Cast('1900-01-01' AS DATE)
ELSE DATE_ADD(Cast('1900-01-01' AS DATE) ,Cast(STG_101_118_INVM.ACCT_OPEN_DT AS INTEGER) - 1) END
AS ACCT_OPEN_DT

Related

SQL function problem - MOD function with data column does not work

I have a problem with function in sql.
SQL function:
SELECT resolved_by ,short_description ,case_id ,STATUS ,create_date ,resolved_date ,resolution, datediff(day, create_date, resolved_date) - 1 - MOD (create_date, 1) + MOD(resolved_date, 1)
FROM booker.o_remedy_tickets
WHERE assigned_to_group IN ( 'emae' ) AND create_date > '2020-11-17'
In excel it works and mod function looks like:
=NETWORKDAYS(E2,F2)-1-MOD(E2,1)+MOD(F2,1)
But I have no idea how to format this mod function to sql.
create_date table is in format like: 2020-11-11 07:58:16.0
resolved_date in format like: 2020-11-11 09:38:38.0
I have error:
Invalid operation: function mod(timestamp without time zone, integer) does not exist.
I am in stuck...
If someone have any idea I really appreciate it.
Thanks a lot!

Error in list of function arguments: '=' not recognised. Unable to parse query in SSRS

Hi i am trying to create a report in SSRS , and i am using below query for taking date difference and comparison.
IIF ((CAST(issues.created_at as smalldatetime)) = (CAST(sprints.created_at as smalldatetime)) OR (DATEDIFF(Minute,CAST(sprints.created_at as smalldatetime), CAST(issues.created_at as smalldatetime)) < 2),1,0) as new_task
but i am getting error "Error in list of function arguments: '=' not recognised. Unable to parse query"
is there anything i need to change in the code.
It seems that your version of SQL Server does not support the IIF (added in 2012).
Change it to a CASE statement which has the same functionality.
SELECT...,
CASE WHEN CAST(issues.created_at AS SMALLDATETIME) = CAST(sprints.created_at AS SMALLDATETIME)
OR DATEDIFF(MINUTE, CAST(sprints.created_at AS SMALLDATETIME), CAST(issues.created_at AS SMALLDATETIME)) < 2
THEN 1 ELSE 0 END AS new_task

Create Sybase function with exception handling

I want to create function to use it in creation of view.
In my table there are strings (strings are consists only of 8 digits) that I'm converting into DATE.
My function is:
CREATE FUNCTION MY.FUNCTION(#date int)
RETURNS DATE
AS
BEGIN
RETURN CONVERT(DATETIME, #date)
END
If I use smth like SELECT FUNCTION('20170323') FROM TABLE it works as expected.
But if I'll try smth like SELECT FUNCTION('77777777') FROM TABLE it fails of course... But if it fail I need to retut NULL!
After some digging I have no result about function modification.
How to add exception handling in my function properly to return date on NULL if it fails?
use TRY_CONVERT instead of CONVERT, which would be :
CREATE FUNCTION TEST(#date varchar(50))
RETURNS DATETIME
AS
BEGIN
RETURN TRY_CONVERT(DATETIME, #date)
END
Result:
select [dbo].[TEST]('20171201') --output:2017-12-01 00:00:00.000
select [dbo].[TEST]('9999999999') --output: NULL
After a long investigation and lot of efforts I've found my solution:
CREATE FUNCTION MY_FUNCTION(#date CHAR(20))
RETURNS DATE
AS
BEGIN
RETURN
(CASE
WHEN ISDATE(#date) = 0
THEN NULL
ELSE CAST(#date AS DATE)
END)
END
Sybase method ISDATE() doing all magic in this case without throwing exception...

SQL WHERE with multiple conditions: shortcut?

I'm trying to find an average temperature on days that are Saturday or Sunday using SQL. I realize a possible solution is:
SELECT avg(cast(meantempi as integer))
FROM weather_data
WHERE cast (strftime('%w', date) as integer) = 0 or
cast (strftime('%w', date) as integer) = 6
However, I am curious as to whether there is an optimal shortcut possible after my WHERE statement in this particular situation.
In other words, is there an equivalent to doing the preceding problem as:
SELECT avg(cast(meantempi as integer))
FROM weather_data
WHERE cast (strftime('%w', date) as integer) = 0 or 6
You can use in:
SELECT avg(cast(meantempi as integer))
FROM weather_data
WHERE cast (strftime('%w', date) as integer) IN (0, 6)

last_day function not working in DB2

Database: DB2 v9.5.301.436
Requirement: I need to find number of days in a month.
Code:
select day(last_day(created))
from tablename
Error:
[Error Code: -440, SQL State: 42884] DB2 SQL Error: SQLCODE=-440, SQLSTATE=42884, SQLERRMC=LAST_DAY;FUNCTION, DRIVER=3.57.82. 2)
[Error Code: -727, SQL State: 56098] DB2 SQL Error: SQLCODE=-727, SQLSTATE=56098, SQLERRMC=2;-440;42884;LAST_DAY|FUNCTION, DRIVER=3.57.82
I checked the DB2 documentation, Which shows above function is available.
http://www-01.ibm.com/support/knowledgecenter/api/content/SSEPEK_10.0.0/com.ibm.db2z10.doc.sqlref/src/tpc/db2z_bif_lastday.html
the following works fine for me.
select day(last_day(current_timestamp))
from SYSIBM.SYSDUMMY1
are you sure created is a date?
The below code worked for me.
select day(date(date(created))-(day(date(created)) -1) days + 1 month -1 day) as maxdate , created from customer ;
Note: created column is a timestamfild
If created is a Date couln use below query.
select day(date(created)-(day(created) -1) days + 1 month -1 day) as maxdate , created from customer ;
last_day where introduced in 9.7. You can roll your own like:
create function myfun.last_day(d date)
returns date
return (d + 1 month) - day(d + 1 month) days
with t(d) as (
values date('2014-01-11')
union all
select d + 1 month
from t
where d<'2015-01-01'
)
select d, myfun.last_day(d) from t