'DECFLOAT' error in db2 - db2

Lookup Error - DB2 Database Error: ERROR [22018] [IBM][DB2/AIX64] SQL0420N Invalid character found in a character string argument of the function "DECFLOAT".
Query-----
SELECT
MSISDN,
CONTRNO,
TRANSDATE,
TARIFF_GROUP,
ACT_DURATION,
BILLTEXT,
GROSS_AMOUNT,
CASE
WHEN TARIFF_GROUP IN('PAG2')
THEN DECIMAL((DECIMAL(ACT_DURATION,10,4)/10),20,4)*0.01
ELSE 'CHECK'
END RA_RATE
FROM HISTCALLS
WHERE call_type IN (50,
54)
AND TRANSDATE = CURRENT date - 1 DAY

The problem is in your case expression.
A single result column cannot be numeric for some rows and character in others.
SELECT
MSISDN,
CONTRNO,
TRANSDATE,
TARIFF_GROUP,
ACT_DURATION,
BILLTEXT,
GROSS_AMOUNT,
CASE
WHEN TARIFF_GROUP = 'PAG2'
THEN DECIMAL(ACT_DURATION * 0.001, 10,4)
ELSE null
END RA_RATE
FROM HISTCALLS
WHERE call_type IN (50, 54)
AND TRANSDATE = CURRENT date - 1 DAY

Related

Postgres CASE WHEN IN query

I've attempted the below query in postgres 11
CASE
WHEN planning_status::varchar in (('Application Under Consideration'::varchar,'Appeal In Progress'::varchar)) then 'this_label'
WHEN planning_status::varchar = 'Approved' and actual_completion_date is not null then 'that_label'
ELSE 'reject_label'
END
I can't get the query to run, initially getting error on mismatching operator to record type. I also attempted IN (VALUES()) method. The below works:
CASE
WHEN planning_status = 'Application Under Consideration' then 'this_label'
WHEN planning_status = 'Appeal In Progress' then 'this_label'
WHEN planning_status = 'Application Received' then 'this_label'
WHEN planning_status = 'Approved' and actual_completion_date is not null then 'that_label'
ELSE 'reject_label'
END
Is it possible to use the IN query within a CASE WHEN query with strings. The strings are categorical but not stored as such
The problem are the double parentheses:
-- this doesn't work:
SELECT CASE WHEN 1 IN ((1, 2)) THEN 'works' ELSE 'weird' END;
ERROR: operator does not exist: integer = record
LINE 1: SELECT CASE WHEN 1 IN ((1, 2)) THEN 'works' ELSE 'weird' END...
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
-- this works:
SELECT CASE WHEN 1 IN (1, 2) THEN 'works' ELSE 'weird' END;
case
═══════
works
(1 row)
The reason is that in the first statement, the inner parentheses are forming a composite type (record) with two elements, and PostgreSQL doesn't know how to compare that to the integer 1.
If the = version works, then this should work:
(CASE WHEN planning_status IN ('Application Under Consideration', 'Appeal In Progress', 'Application Received')
THEN 'this_label'
WHEN planning_status = 'Approved' and actual_completion_date is not null
THEN 'that_label'
ELSE 'reject_label'
END)
There should be no need for an explicit type conversion. If this doesn't work, what is the type of planning_status?

case when in where clause postresql

select service_code,service_cat_code,mobile_no,upper(applicant_name_eng) as name,to_char(license_date,'dd/mm/yyyy')as license_from,to_char(license_valid_upto,'dd/mm/yyyy')as license_to,Upper(license_no),district_code,taluk_code,CONCAT(address_building,', ', address_cityvillage,', ',address_locality,', ',address_landmark,', ',address_street) as address
from mst_license
WHERE cast(license_valid_upto as date) = case
WHEN license_valid_upto < now()
THEN
case
when license_valid_upto = '2021-06-30'
then 1 else 0
END
ELSE
case when license_valid_upto > now()
then 1 else 0
End
END
and Upper(license_no)='1SP146924BJP'
I want license valid should be either greater than now or if license valid less than now it must be with the date ''30/06/2021' but when i use above query i get error
ERROR: operator does not exist: date = integer
LINE 3: WHERE cast(license_valid_upto as date) = case
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 418
Help me out guys
The main issue you have is that your case statement returns an integer (1 or 0) but you are trying to compare that to a date, which you cannot do as Postgres is a strict data typing. Even if it did work it would always be false (well except for 1969-12-31/1970-01-01). Moreover the case structure is not needed. The best/correct to compare dates is just use date values. Since you did not indicate the data type for column license_valid_upto so based on how it is used I'll assume it is timestamp with timezone as that is what NOW() returns. Your query becomes:
select service_code
, service_cat_code
, mobile_no
, upper(applicant_name_eng) as name
, to_char(license_date,'dd/mm/yyyy')as license_from
, to_char(license_valid_upto,'dd/mm/yyyy')as license_to
, upper(license_no) as license_no
, district_code
, taluk_code
, concat(address_building,', '
,address_cityvillage,', '
,address_locality,', '
,address_landmark,', '
,address_street) as address
from mst_license
where and upper(license_no)='1SP146924BJP'
and ( cast(license_valid_upto as date) > cast( now() as date)
or (cast (icense_valid_upto as date) < cast( now() as date)
and cast (icense_valid_upto as date) = date '2021-06-30'
)
);
Also, learn for format your queries for readability and so you do not need to scroll right. You, and others looking at your queries later will appreciate it later.

Cast integer to decimal in DQL

I use Doctrine with a Postgres database and want to update the integer field "voting".
It's a procentual value, saved as integer between 0 and 100, based on the two integer fields "voteCountPro" and "voteCount".
I have to cast one of the integers to a decimal value. (See: Division ( / ) not giving my answer in postgresql)
This doesn't work in DQL and fails with the message:
[Syntax Error] line 0, col 363: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got ':'
UPDATE statement s
SET s.voting = (s.voteCountPro::decimal / s.voteCount) * 100
WHERE s.id = :id
How can I set the value?
Install https://github.com/oroinc/doctrine-extensions, register the CAST function and write:
UPDATE statement s
SET s.voting = (CAST(s.voteCountPro as decimal) / s.voteCount) * 100
WHERE s.id = :id

normalize date in postgres - character to date

I have a Table with coloumnname description
description //character varying
LZ000834_28-02-14
LZ000834_28-02-14
LA20683_30-04-15
LA20683_30-04-15
LA20300_31-01-15
LA20300_31-01-2015
LA20264_31-01-15
LA20264_31-01-2016
LAN2078_31-03-16
LAN2078_31-03-15
LAN8394_31-04-14
L2Z82736_31_03_2015 //has 1million rows
here description means batchname_expirydate
my question is how do I normalize my description column convert all those dates to DD-MM-YY format
i have tried this two queries
select substring(description from position('_' in description) +1) from attributesetinstance;
above query will give me all the strings then i tried date conversion like this
select to_date(substring(description from position('_' in description) +1), 'DD-MM-YY') from attributesetinstance;
now this gives me error
ERROR: invalid value "_3" for "DD"
DETAIL: Value must be an integer.
********** Error **********
ERROR: invalid value "_3" for "DD"
SQL state: 22007
Detail: Value must be an integer.
how do update/recorrect all my database ?
update:
tried with another sql
with product_dates AS (
select description, ((val[2])||'-'||val[3]||'-'||val[4])::date as expir_date
from (
select description,regexp_matches(description, '([a-zA-Z0-9]+)_([0-9]+)[-_]([0-9]+)[-_]([0-9]+)') as val
from attributesetinstance
) a
), expiring_dates AS (
select description from product_dates
)
select description from expiring_dates
i get following error:
ERROR: date/time field value out of range: "31-04-14"
********** Error **********
ERROR: date/time field value out of range: "31-04-14"
SQL state: 22008
update
my postgres datestyle
show datestyle;
"ISO, DMY"
This error message is not well - this date 2014-04-31 is invalid. So you cannot to cast this string to date with algorithm, that you used. But to_date function is fault tolerant
postgres=# select '2014-04-31'::date;
ERROR: date/time field value out of range: "2014-04-31"
LINE 1: select '2014-04-31'::date;
^
Time: 0.551 ms
postgres=# select to_date('2014-04-31','YYYY-MM-DD');
to_date
────────────
2014-05-01
(1 row)
This code works
postgres=# select to_date(replace(substring('LA20683_30_04_15' from '\d+[-_]\d+[-_]\d+$'),'_','-'), 'DD-MM-YY');
to_date
────────────
2015-04-30
(1 row)
Time: 57.840 ms
postgres=# select to_date(replace(substring('LA20683_30_04_2015' from '\d+[-_]\d+[-_]\d+$'),'_','-'), 'DD-MM-YY');
to_date
────────────
2015-04-30
(1 row)
workaround for 8.4:
CREATE OR REPLACE FUNCTION to_date_DD_MM_YY_2_4(text)
RETURNS date AS $$
SELECT CASE WHEN $1 ~ e'\\d+-\\d+-\\d{2}$' THEN to_date($1, 'DD-MM-YY')
ELSE to_date($1, 'DD-MM-YYYY')
END$$
LANGUAGE sql;
CREATE FUNCTION
Time: 25.229 ms
postgres=# SELECT to_date_DD_MM_YY_2_4('30-04-2015');
to_date_dd_mm_yy_2_4
----------------------
2015-04-30
(1 row)

column "statement_date" is of type date but expression is of type integer

Consider the following query:
INSERT INTO statement_line_items
SELECT count(*)::integer as clicks, sum(amount_cents)::integer as amount_cents, imps.user_id, imps.created_at::date as statement_date
FROM impression_events imps
INNER JOIN transactions t ON t.event_id = imps.id
AND t.event_type = 'ImpressionEvent'
AND amount_cents >= 0
WHERE imps.created_at >= (now() - interval '8 days')::date
AND imps.created_at < (now() - interval '7 day')::date
AND imps.clicked = true
GROUP BY imps.user_id, imps.created_at::date;
This is returning:
ERROR: column "statement_date" is of type date but expression is of type integer
LINE 2: ...icks, sum(amount_cents)::integer as amount_cents, imps.user_...
^
HINT: You will need to rewrite or cast the expression.
********** Error **********
ERROR: column "statement_date" is of type date but expression is of type integer
SQL state: 42804
Hint: You will need to rewrite or cast the expression.
Character: 117
My table structure for statement_line_items is:
"id"; "integer"
"user_id"; "integer"
"statement_date"; "date"
"description"; "character varying(255)"
"clicks"; "integer"
"amount_cents"; "integer"
"created_at"; "timestamp without time zone"
"updated_at"; "timestamp without time zone"
You are putting the imps.userid in your statement_date column. That has to fail.
count(*)::integer as clicks goes into id
sum(amount_cents)::integer as amount_cents goes into userid
imps.user_id goes into statement_date
To specify the order you are inserting you can do this:
INSERT INTO statement_line_items (col1, col2, ...)
values (select data_for_col1, data_for_col2, ...)
If you have this kind of problem check if your date is in the apostrophe.