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

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!

Related

Convert query Teradata to 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

Converting decimal to Date

I have a column with dates formatted as decimals, for example: 20,210,830.
I want to convert this number to date format as 08/30/2021
I have tried to use convert and the database shoots me an error that convert is not a valid function. Cast seems to work but, only returns a null value every time.
This statement will validate:
SELECT CAST(CAST(CONTCLMPDTE AS VARCHAR(8)) AS DATE)
FROM CMSFIL.JCTDSC AS COMPLDATE
This statement works but, just outputs null. For background I am querying from a Db2 database.
My ultimate goal is to use this converted date to grab the difference from the current day.
Such as
DAY(CURRENT_DATE) - DAY(COMPLDATE)
Converting it to a date, you cqan do it like this
CREATE TABLE JCTDSC (
CONTCLMPDTE varchar(10)
);
INSERT INTO JCTDSC VALUES ('20,220,830')
SELECT date(to_date(REPLACE(CONTCLMPDTE,',',''),'YYYYMMDD')) FROM JCTDSC AS COMPLDATE
1
2022-08-30
fiddle
So after a long couple days and almost pulling my hair out, here is what worked for me.
SELECT date(substr(CONTCLMPDTE,1,4)||'-'||substr(CONTCLMPDTE,5,2)||'-'||substr(CONTCLMPDTE,7,2)) FROM JCTDSC WHERE COMPANYNUMBER={Company Number} AND JOBNUMBER={Job Number} LIMIT 1
This formatted from yyyymmdd to mm/dd/yyyy. It also worked for finding the days between current_date and CONTCLMPDTE using this code.
DAYS(CURRENT_DATE) - DAYS({COntract Compl Date Formatted})
Thank you all for your help!
You probably get an error because you have some INT / DECIMAL value which can't be converted to a date using this pattern.
The solution is to create some "safe" conversion function "eating" errors like below.
CREATE FUNCTION DATE_SAFE (P_DT INT)
RETURNS DATE
CONTAINS SQL
NO EXTERNAL ACTION
DETERMINISTIC
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
RETURN CAST (NULL AS DATE);
END;
RETURN DATE (TO_DATE (CAST (P_DT AS CHAR (8)), 'YYYYMMDD'));
END
Usage:
SELECT
CONTCLMPDTE
--, DATE (TO_DATE (CAST (CONTCLMPDTE AS CHAR (8)), 'YYYYMMDD'))
, DATE_SAFE (CONTCLMPDTE)
FROM (VALUES 0, 20220830) T (CONTCLMPDTE)
The function returns NULL if the corresponding INT can't be converted to a DATE, and no error is thrown as in case, when you uncomment the commented out line with built-in functions only.
The value just need to be converted into a string with a date format. Then you can use the date() function to convert to date.
create table qtemp/dec_vals (
col1 decimal(8,0) );
insert into qtemp/dec_vals
values (20200830), (20200831), (20200901), (20200902), (20200903), (20200904), (20200905), (20200906);
select date(substr(char(col1), 5, 2) || '/' || substr(char(col1), 7, 2) || '/' || substr(char(col1), 1, 4)) from qtemp/dec_vals;

Date to Integer YYYMMDD in Standar SQL (BigQuery)

I need to convert a date to an integer with the format YYYMMDD in Bigquery.
I tried with:
PARSE_DATE('%Y%m%d', response_date)
but its not working (Error: No matching signature for function PARSE_DATE for argument types: STRING, DATE. Supported signature: PARSE_DATE(STRING, STRING) at [1:8])
response_date: Date Format
Wanted Result: 20210201 as an integer or string
If anyone know the correct sintax it would be really helpful. Thank you!
One method is arithmetic:
select extract(year from response_date) * 10000 + extract(month from response_date) * 100 + extract(day from response_date)
Another method is FORMAT_DATE():
select cast(format_date('%Y%m%d', response_date) as int64)
Another option (might look funny/silly but works)
select translate('' || response_date, '-', '')
and then you can do with resulted string whatever cast'ing you need or just leave as is (as string)
... can easily be tested with below query
select translate('' || current_date(), '-', '')
with output
But obviously - the best way to go is with FORMAT_DATE() as in Gordon's answer :o)

postgresql: syntax error for creating a function that determines the differences in dates

I would like to write a function that calculates the difference in two dates. Here is my function:
CREATE OR REPLACE FUNCTION order_prep_days(ship_date DATE, order_date DATE)
returns
'ship_date - order_date'
language sql
select pct_change(2010-08-01, 2010-06-30);
Then I got this error:
syntax error at or near "'ship_date - order_date'"
LINE 3: 'ship_date - order_date'
I would hugely appreciate it if someone can help me get the output without any error. Thank you very much in advance!
There are multiple issue with your code, notably:
the datatype of the arguments should be dates rather than numeric
you need to declare the return type, and use the as keyword to before the actual code starts
you should select the substraction operation
when invoking the function, arguments need to be quoted
Consider:
create or replace function order_prep_days(ship_date date, order_date date)
returns integer as 'select ship_date - order_date'
language sql
select order_prep_days('2010-08-01', '2010-06-30');
Yields:
| order_prep_days |
| --------------: |
| 32 |
Demo on DB Fiddle
I think you want something like:
CREATE OR REPLACE FUNCTION order_prep_days(ship_date DATE, order_date DATE)
returns int as
'select ship_date - order_date'
language sql
See SQLFiddle

how to get to_epoch(sysdate-90) in Hive

I have a query which runs good in Oracle, but i want to use the same query in Hive.
query:
select count(mem_id)
from mem
where cobrand_id = '10001372'
and user_type_id =1
and status_ind <>3
and LAST_ACCESSED >= to_epoch(sysdate-90);
Also, I have LAST_ACCESSED coulmn in double.example value of LAST_ACCEESSED is : 1.554386487E9, not sure what value this is i am guessing this could be seconds.
tried:
UNIX_TIMESTAMP( string date, string pattern )
FROM_UNIXTIME( bigint number_of_seconds [, string format] )
No luck. Can someone help me. Thanks in advance.
It seems 1.554386487E9 is the same unix epoch time stored in double, displayed in engineering notation, and it can be casted to BIGINT.
Checking your example:
select from_unixtime(cast(1.554386487E9 as bigint));
OK
2019-04-04 07:01:27
Does this timestamp look good?
If yes, then use unix_timestamp(concat(date_sub(current_date,90),' 00:00:00')) to get epoch time for current date - 90 days.
Your query fixed:
select count(mem_id)
from mem
where cobrand_id = '10001372'
and user_type_id =1
and status_ind <>3
and cast(LAST_ACCESSED as BIGINT) >= unix_timestamp(concat(date_sub(current_date,90),' 00:00:00'))