invalid input syntax for integer: "" : Postgresql - postgresql

I am doing a migration from mssql to postgresql and I am facing issue in resolving one of the queries.
My Query:
SELECT 1,
CASE
WHEN cast((case
when split_part(mt_outward_qty,'/',1) > '0' then
substring(mt_outward_qty,0,split_part(mt_outward_qty,'/',1)::int)
when split_part(mt_outward_qty,'.',1)>'0' then
substring(mt_outward_qty,0,split_part(mt_outward_qty,'.',1)::int)
else
mt_outward_qty
end) as int) > 0
THEN 'Y'
ELSE 'N'
END
from STL_CS_Tra_requestdetails
LEFT JOIN STL_CS_Tra_storeopening ON req_id = so_requestid
LEFT JOIN STL_CS_Tra_multiple_timinig ON req_id = mt_reqid
Upon executing this, I am getting:
invalid input syntax for integer: ""
Kindly help me in resolving this.

sql server automatically cast empty string to 0 when you cast to int
you can do it in postgresql like so :
( 0 || '')::int

Related

Database-migration ms sql to postgresql

I am getting a syntax error when converting between SQL Server to PostgreSQL. Any thoughts?
IF (var_port_with_bmrk_total_mv != 0 AND var_bmrk_info IS NOT NULL) THEN
BEGIN
insert into t$tmp_diff
select #asof_dt asof_dt,#choiceID choiceID ,p.input_array_type ,p.group_order, CONVERT(DECIMAL(32,10),p.port_value/#var_port_total_mv) port_value,convert(decimal(32,10), isnull(bmrk_value/#port_with_bmrk_total_mv,0)) bmrk_value
from t$tmp_port_sum p, t$tmp_bmrk_sum b
where p.input_array_type=b.input_array_type and p.group_order = b.group_order
END;
ELSE
Original before conversion
insert into #tmp_other_diff
select #asof_dt asof_dt,#choiceID choiceID , b.input_array_type,b.grouping,convert(decimal(32,10),0) port_value, (bmrk_value/#port_with_bmrk_total_mv) bmrk_value
from #tmp_bmrk_other_sum b
where b.key_value not in ( select p.key_value from #tmp_port_other_sum p)
Error message:
Error occurred during SQL query execution
Reason:
SQL Error [42601]: ERROR: syntax error at or near ","
Position: 9030
the relevant comma being:
CONVERT(DECIMAL(32,10),p.port_value
There is no convert() function in Postgres. Use the SQL standard cast or the Postgres extension ::data type. In this case:
...., cast(0 as decimal(30,10)) port_value, ....
OR
...., 0::decimal(30,10) port_value, ...
Note: No comma after the expression. In the original port_value is the column alias. You need to keep it that way.

Missing right paranthesis in Crystal report with Oracle

I have the following query to get the data between dates. If I execute with values in Oracle then it is returning the data. But while developing the crystal report it is throwing missing parenthesis. I was stuck and client already started screaming. Please help out.. :(
Parameters
P_GEN_REPT_ID : STRING
P_START_DATE : DATE :MM/DD/YYYY
P_END DATE : DATE :MM/DD/YYYY
P_KPI_ONLY : STRING
P_RLTM_EQP_TYPE : STRING
SELECT DISTINCT a.f_cd
FROM realtime_equip_installations a
WHERE a.ep_a_num IN
(SELECT codes
FROM scsi_driving_lists s
WHERE s.rpt_run_id = '{?p_gen_rpt_run_id}')
AND instr(nvl('{?p_rltm_eqp_type}',
a.eqp_typ_cd),
a.eqp_typ_cd) > 0
AND (nvl(a.eqp_inst_dt,TO_DATE('{?p_start_date}','MM/DD/YYYY'))
BETWEEN TO_DATE('{?p_start_date}','MM/DD/YYYY')
AND TO_DATE('{?p_End_date}','MM/DD/YYYY'))
AND ('{?p_kpi_only}' = 'N' OR a.eqp_pe_vrfy_flg = 'Y')
AND A.EQP_TYP_CD != 'MPFM'
UNION
SELECT w.f_cd
FROM multiphase_flowmeters m,
pipelines p,
realtime_equip_installations a,
well_master w
WHERE p.ds_node_id = to_char(m.mpfm_num)
AND a.mpfm_num = m.mpfm_num
AND w.ep_a_num = p.us_node_id
AND p.us_node_typ_cd = 'WELL'
AND p.ds_node_typ_cd = 'MPFM'
AND p.us_node_id IN
(SELECT codes
FROM scsi_driving_lists s
WHERE s.rpt_run_id = '{?p_gen_rpt_run_id}')
AND instr(nvl('{?p_rltm_eqp_type}',
a.eqp_typ_cd),
a.eqp_typ_cd) > 0
AND (nvl(a.eqp_inst_dt,TO_DATE('{?p_start_date}','MM/DD/YYYY'))
BETWEEN TO_DATE('{?p_start_date}','MM/DD/YYYY') AND
TO_DATE('{?p_End_date}','MM/DD/YYYY'))
AND ('{?p_kpi_only}' = 'N' OR a.eqp_pe_vrfy_flg = 'Y')
ORDER BY f_cd
Its showing missing right parenthesis.
Each of the inner queries starting with:
WHERE a.ep_a_num IN (SELECT ...
and
IN (SELECT codes ...
needs a closing parenthesis

ERROR: invalid input syntax for type timestamp:

I am getting an Error in my PostgreSQL query as
ERROR: invalid input syntax for type timestamp:
I am setting the value using \set.
\set dueDateEarliest '2018-04-01'
\set dueDateLatest '2018-08-01'
\set dueDateLatest '2018-08-01'
And trying to use these value in my query as below
SELECT DISTINCT(bu.id) as "user_id",c.organization_name as "name",round(i.balance,2) as "amount_due",i.id as "invoice_number",i.due_date as "due_date",CONCAT('collectionMonth', LPAD(cf2.content,2,'0')) as "collection_date" FROM base_user bu,contact c,contact_field cf, invoice i, contact_field cf2 WHERE bu.id = c.user_id AND bu.deleted = 0 AND cf.contact_id = c.id AND cf.type_id = 7 AND cf.content = 'DD' AND i.user_id = bu.id AND i.balance > 0 AND i.is_review != 1 AND i.deleted != 1 AND due_date BETWEEN 'dueDateEarliest' AND 'dueDateLatest' AND cf2.contact_id = c.id AND cf2.type_id = 8 ORDER BY bu.id limit 20;
This is giving error as
ERROR: invalid input syntax for type timestamp:
I am not getting any way to fix it.
And moreover the way I am setting value using \set is it fine ?
Or should I use SET to set the values.
Because in actual when I have to run these command from a shell script I will be calling/setting as
`set dueDateEarliest '$dueDateEarliest'` from shell script.
Which is the best way ?
Attaching the screen shot as well
It's an issue with how you formatted your query. Let's simplify it a bit:
# \set dueDateEarliest '2018-04-01'
# select 'dueDateEarliest'::timestamp;
ERROR: invalid input syntax for type timestamp: "dueDateEarliest"
LINE 1: select 'dueDateEarliest'::timestamp;
It doesn't work, because it's trying to use the string 'dueDateEarliest', not the variable.
Here's the correct way:
# select :'dueDateEarliest'::timestamp;
timestamp
---------------------
2018-04-01 00:00:00
(1 row)

PostgreSQL: return message after count = 0

I have maybe easy question, but I'm completely stucked.
I have script
SELECT COALESCE(COUNT(id), 0) as MyFiels from table
It works fine and when I have zero value it shows 0.
But I want that instead of 0, I can see one line = "NO RESULTS" for example.
I tried:
SELECT COALESCE(to_char(COUNT(id), 'NO RESULT')) as MyFiels from table
And PostgreSQL shows error message:
ERROR: "E" is not supported
SQL state: 0A000
Where I'm incorrect? Any ideas?
I see what is the error, you are trying to use coalesce to convert 0 to string, and coalesce convert null to something. You need use a CASE
SELECT CASE WHEN COUNT(*) = 0 THEN 'NO RESULT'
ELSE CAST(COUNT(*) as TEXT)
END as field
FROM Table

need to translate specific t-sql case in pl/sql

Can anyone tell me how to translate the following T-SQL statement:
SELECT fileld1 = CASE
WHEN T.option1 THEN -1
ELSE
CASE WHEN T.option2 THEN 0
ELSE 1
END
END
FROM Table1 AS T
The point is I need to validate two different options from the table for a single field in the select statement..
I have tried to do somthing with an IF statement in pl/sql, but it just doesnt work for me:
SELECT IF T.option1 THEN -1
ELSE IF T.option2 THEN 0
ELSE 1
END
FROM Table1 AS T
I am not actually sure how to write IF statement inside the SELECT statement..
And also, I need to do it INSIDE the select statement because I am constructing a view.
Use:
SELECT CASE
WHEN T.option1 = ? THEN -1
WHEN T.option2 = ? THEN 0
ELSE 1
END AS field1
FROM Table1 AS T
I can't get your original TSQL to work - I get:
Msg 4145, Level 15, State 1, Line 4
An expression of non-boolean type specified in a context where a condition is expected, near 'THEN'.
...because there's no value evaluation. If you're checking if the columns are null, you'll need to use:
SELECT CASE
WHEN T.option1 IS NULL THEN -1
WHEN T.option2 IS NULL THEN 0
ELSE 1
END AS field1
FROM Table1 AS T
...or if you need when they are not null:
SELECT CASE
WHEN T.option1 IS NOT NULL THEN -1
WHEN T.option2 IS NOT NULL THEN 0
ELSE 1
END AS field1
FROM Table1 AS T
CASE expressions shortcircuit - if the first WHEN matches, it returns the value & exits handling for that row - so the options afterwards aren't considered.
If I remember correctly, PL/SQL also supports the case. You just would have to move the column alias from "field1=" before the expression to "AS filed1" after the expression.