Postgresql Syntax error at or near " THEN " - sql

I'm trying to create a view in Postgresql , but when I run this code appears this error:
syntax error at or near " THEN "
CREATE OR REPLACE VIEW VW_MONITOR_DEVICE AS
SELECT
P.POSIZIONE_DEVICE_ID AS MONITOR_DEVICE_ID,
P.VALID AS VALID,
[...]
IF (VALID == FALSE THEN 'Valid' ELSE P.REASON_FOR_INVALID) AS DESCRIPTION,
[...]
FROM public.TA_POSIZIONI_DEVICE P
JOIN ...
TA_POSIZIONI_DEVICE
VALID (Boolean not null)

You should use CASE
The SQL CASE expression is a generic conditional expression, similar
to if/else statements in other programming languages
CASE WHEN condition THEN result
[WHEN ...]
[ELSE result]
END
So,
CREATE OR REPLACE VIEW VW_MONITOR_DEVICE AS
SELECT
P.POSIZIONE_DEVICE_ID AS MONITOR_DEVICE_ID,
P.VALID AS VALID,
[...]
CASE WHEN VALID = false THEN 'Valid'
ELSE P.REASON_FOR_INVALID
END AS DESCRIPTION,
[...]
FROM public.TA_POSIZIONI_DEVICE P
JOIN ...

you can use case
case when VALID = FALSE THEN 'Valid' ELSE P.REASON_FOR_INVALID end DESCRIPTION,

IF (VALID == FALSE) THEN 'Valid' ELSE P.REASON_FOR_INVALID END IF AS DESCRIPTION
Try this ! You've got a parenthensis issue in you condition

Related

How do I use conditions properly in PSQL?

I wanted to do a condition wherein I put values (000000) in DATE_COMPLETED if it see's the FLAG_CLOSED = Y and if its not Y then do nothing
SELECT
"JOB",
"SUFFIX",
"SUFFIX",
"DATE_COMPLETED",
"FLAG_CLOSED",
CASE "DATE_COMPLETED"
WHEN "FLAG_CLOSED"='Y'
THEN "DATE_COMPLETED"='000000'
END "DATE_COMPLETED"
FROM "JOB_OPERATIONS"
What I got
SQL Execution Error
[LNA][PSQL][SQL Engine]Syntax Error: SELECT
"JOB",
"SUFFIX",
"SUFFIX",
"DATE_COMPLETED",
"FLAG_CLOSED",
CASE "DATE_COMPLETED" WHEN "FLAG_CLOSED" << ??? >> = 'Y' THEN "DATE_COMPLETED" = '000000' END "DATE_COMPLETED"
FROM JOB_OPERATIONS
It looks like you're attempting to change the DATE_COMPLETED column in your table. You can't do that with a SELECT statement. CASE / WHEN / THEN helps construct output. UPDATE statements allow clauses like DATE_COMPLETED='000000' that change columns.
Try something like this.
SELECT "JOB", "SUFFIX", "SUFFIX", "DATE_COMPLETED", "FLAG_CLOSED",
CASE WHEN "FLAG_CLOSED"='Y' THEN '000000'
ELSE "DATE_COMPLETED" END "CLOSED_DATE_COMPLETED"
FROM "JOB_OPERATIONS"
I named your CASE-computed output column CLOSED_DATE_COMPLETED so it won't collide with the DATE_COMPLETED colum you already mentioned.
Syntax is either:
CASE a WHEN b
... or:
CASE when a=b
To return the value of DATE_COMPLETED depending on the flag, you can do this:
CASE "FLAG_CLOSED"
WHEN 'Y' THEN '000000'
ELSE "DATE_COMPLETED"
END AS "DATE_COMPLETED"
Beware that you need to produce a coherent column type. If DATE_COMPLETED is not text, you'll need to cast it.

Postgres SQL state: 22P02 - invalid input syntax for integer

I'm using a sql query to export a database from my company's program.
Everything seems to be fine till I change the date on the "where" statement with a previous one.
Please find below the code:
SELECT p."Index", p."PSN" || CAST(p."PNR"as int) AS ID,
p."PSN" AS Serie, cast(p."PNR"as int) AS Numar,
pr."PINDate" AS r_gdate,
CASE WHEN pr."AsigEID"='10' THEN pr."PrimSUM" ELSE
pr."PrimSUM"*valuta1."EXCValue" END AS r_prima_lei,
CASE WHEN pr."AsigEID"='2'
THEN pr."PrimSUM"
ELSE CASE WHEN pr."AsigEID"='10' THEN pr."PrimSUM"/valuta2."EXCValue"
ELSE pr."PrimSUM"*valuta1."EXCValue"/valuta2."EXCValue"
END
END AS r_prima_eur,
CASE WHEN pr."AsigEID"='10' THEN pr."AsigSUM" ELSE
pr."AsigSUM"*valuta1."EXCValue" END as r_sa_lei,
CASE WHEN pr."AsigEID"='2'
THEN pr."AsigSUM"
ELSE CASE WHEN pr."AsigEID"='10' THEN pr."AsigSUM"/valuta2."EXCValue"
ELSE pr."AsigSUM"*valuta1."EXCValue"/valuta2."EXCValue"
END
END AS r_sa_eur,
pr."AsigStart", pr."AsigEnd", risc."Code", plink."Index"
FROM "PolsRisc" AS pr
LEFT JOIN "Pols" as p ON p."Index" = pr."PID"
LEFT JOIN "Riscs" as risc ON pr."RID" = risc."Index"
LEFT JOIN "PRLNK" plink ON plink."PTID" = p."PTID" AND plink."RID" = risc."Index"
LEFT JOIN "EXCValues" valuta1 ON valuta1."AtDate" = pr."AsigStart" AND valuta1."EID" = pr."AsigEID"
LEFT JOIN "EXCValues" valuta2 ON valuta2."AtDate" = pr."AsigStart" AND valuta2."EID"='2'
WHERE pr."PINDate" > '2020-08-01' AND pr."IsRezil" = 'false';
When I'm using '2020-08-01' the query works well. When I try to change it to a previous one eg. '2010-01-01' a get an error:
ERROR: invalid input syntax for integer: ""
SQL state: 22P02
I was looking for a solution on the previous posts but I didn't manage to solve this issue.
It looks like it is returning "" or a null value into one of the columns you are using integer logic for. The date change is just filtering out the data that would crash it.
You may need to use coalesce to reassign the nulls as 0 and then cast it back into being an int
select
cast(coalesce(table.column, 0) as int) as result
from table
I would advice to read the chapter http://www.postgresql.org/docs/current/interactive/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS
. It's a brief and informative read.The cause for the error message is that '' is an empty string that has no representation in a numeric type like integer

"\N" within a case when statement

What is the best way to use a case when statement that employs a "\N" ? I have a column of State data in which a "\N" populates when no state exists (for foreign countries). I'm trying to write a smile case when statement that changes the \N's to "unknown" but am receiving a Error: Invalid string literal: "\N". Is there a way to get around this?
SELECT
STATE_REGION, case when STATE_REGION = "\N" then "unknown" else 'STATE_REGION' end as state_region_two
FROM xxx
Below is for BigQuery Standard SQL
#standardSQL
SELECT STATE_REGION,
CASE
WHEN STATE_REGION = "\\N" THEN "unknown"
ELSE 'STATE_REGION'
END AS state_region_two
FROM `project.dataset.table`
... yet another option is
WHEN STATE_REGION = r"\N" THEN "unknown"

ERROR: operator does not exist: character varying = boolean

I have the following query:
SELECT
CASE
tbl_appointment_types."name"
WHEN tbl_appointment_types."name" LIKE 'Refill-ART' THEN
'Re-Fill'
WHEN tbl_appointment_types."name" = 'Enhanced Adherence'::character varying THEN
'Enhanced Adherence'
WHEN tbl_appointment_types."name" = 'PHARMACY APPOINTMENT'::character varying THEN
'Re-Fill' ELSE tbl_appointment_types."name"
END
FROM
tbl_appointment INNER JOIN tbl_appointment_types ON tbl_appointment_types."id" = tbl_appointment.app_type_1
GROUP BY
tbl_appointment_types."name";
When I try running it, I get the following error:
> ERROR: operator does not exist: character varying = boolean
LINE 4: WHEN tbl_appointment_types."name" LIKE 'Refill-ART' THEN
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
> Time: 0.054s
How can I solve the following issue?
The CASE expression comes in two flavors:
A "short" form that only supports equality where you first state the expression and then the values to which that should be compared (using =), e.g.
CASE some_column
WHEN 1 THEN 'One'
WHEN 2 THEN 'Two'
ELSE 'Something else'
END
Or a more verbose version where you can use different condition for each WHEN. But in that case you can not have the expression after the CASE:
CASE -- nothing here!
WHEN some_column = 1 THEN 'One'
WHEN some_column = 2 THEN 'Two'
ELSE 'Something else'
END
So you need to write your CASE expression as:
CASE -- nothing here!
WHEN tbl_appointment_types."name" LIKE 'Refill-ART' THEN 'Re-Fill'
WHEN tbl_appointment_types."name" = 'Enhanced Adherence' THEN 'Enhanced Adherence'
WHEN tbl_appointment_types."name" = 'PHARMACY APPOINTMENT' THEN 'Re-Fill'
ELSE tbl_appointment_types."name"
END
Using LIKE without a wildcard doesn't really makes sense. "name" LIKE 'Refill-ART' is the same as "name" = 'Refill-ART'.
You probably meant "name" LIKE 'Refill-ART%' (note the % at the end)

Case statement don't work correctly

SQL SERVER 2012
In my case server conflict with '='.
why it is not working?
here is my code:
SELECT
TM.CODE_3 as Account,
TM.NAME_3,
TM.CODE_5,
case TM.Line
when TM.CODE_5 = '9491'--and RS.SUB1Sel = ('05.11 Penalties and fines')
then 'R0820-5'
else TABLE_MAIN_new.Line
end
FROM TABLE_Main_new as TM
left join Danone_Main as DM
on TM.CODE_3 = DM.CODE_3
left join Rep_Struct_2012_N as RS
on TM.CODE_3 = RS.CODE_3
Incorrect syntax near '='.
Try this
case
when TM.CODE_5 = '9491'--and RS.SUB1Sel = ('05.11 Penalties and fines')
then 'R0820-5'
else TABLE_MAIN_new.Line
End
This should work:
CASE TM.CODE_5
WHEN '9491'--and RS.SUB1Sel = ('05.11 Penalties and fines')
THEN 'R0820-5'
ELSE TABLE_MAIN_new.Line
END
When you use CASE expression, the WHEN clause should only be a value (true/false depending on your CASE expression). If no CASE expression is used then the WHEN should be a boolean expression. More info HERE.