ERROR: operator does not exist: character varying = boolean - sql

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)

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

CASE WHEN SQL Syntax Error

I am attempting to us the CASE statement for the first time and I cannot understand why I am getting a syntax error on the last WHEN and ELSE. I am trying to extract a substring if a value starts with a specific set of characters. My Code is below:
SELECT
CASE
WHEN LEFT ([RCode],2) = 'BB' THEN SUBSTRING([RCode],3,LEN([RCode]))
WHEN LEFT ([RCode],4) = 'APT-' THEN SUBSTRING([RCode],5,LEN([RCode])
WHEN LEFT ([RCode],4) = 'PS-' THEN SUBSTRING([RCode],4,LEN([RCode])
ELSE [RCode]
END
FROM [Xperdyte].[dbo].[tJCLines]
Any guidance would be appreciated.
This won't (necessarily) fix your syntax problem, but I would recommend that you use like for the comparisons:
SELECT (CASE WHEN RCode LIKE 'BB%' THEN SUBSTRING([RCode], 3, LEN([RCode]))
WHEN RCode LIKE 'APT-%' THEN SUBSTRING([RCode], 5, LEN([RCode]))
WHEN RCode LIKE 'PS-%' THEN SUBSTRING([RCode], 4, LEN([RCode]))
ELSE [RCode]
END)
FROM [Xperdyte].[dbo].[tJCLines];
Then you don't have to count characters -- and the third condition will match.
Missed off two right parenthesis.
SELECT CASE WHEN LEFT([RCode],2) = 'BB'
THEN SUBSTRING([RCode],3,LEN([RCode]))
WHEN LEFT([RCode],4) = 'APT-'
THEN SUBSTRING([RCode],5,LEN([RCode]))
WHEN LEFT([RCode],4) = 'PS-'
THEN SUBSTRING([RCode],4,LEN([RCode]))
ELSE [RCode]
END
FROM [Xperdyte].[dbo].[tJCLines]

how to prevent converting the text into boolean by the use of when statement in postgresql?

select fti.pa_serial_,fti.homeownerm_name,fti.ward_,fti.villagetole,fti.status,
ftrq.date_reporting, ftrq.name_of_recorder_reporting,
case
when fti.status='terminate' then ftrq.is_the_site_cleared ='1' end as is_the_site_cleared from fti join ftrq on ftrq.fulcrum_parent_id = fti.fulcrum_id
Here, is_the_site_cleared is text type of column which is converted into boolean by the when statement written and hence does not print as 1 and takes as true. I explicitly used print '1'. But this also did not work. My aim is to display '1' in the column 'is_the_site_cleared' when the value of fti.status='terminate'. Please help!!!
How about using integers rather than booleans?
select fti.pa_serial_, fti.homeownerm_name, fti.ward_,
fti.villagetole, fti.status, ftrq.date_reporting,
ftrq.name_of_recorder_reporting,
(case when fti.status = 'terminate' -- and ftrq.is_the_site_cleared = '1'
then 1 else 0
end) as is_the_site_cleared
from fti join
ftrq
on ftrq.fulcrum_parent_id = fti.fulcrum_id ;
From the description, I cannot tell if you want to include the condition ftrq.is_the_site_cleared = '1' in the when condition. But the idea is to have the then and else return numbers if that is what you want to see.

Postgresql Syntax error at or near " THEN "

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