So I have
WITH input_students as (select *
from json_to_recordset(json_data :: json -> 'users')
as student (organization_id bigint, section text, class text,admission_number text))
Now , I wanna make sure that the organization_id is not "" (if so , null would be preferable as a value),however I am not able to use case when ... then end inside of arguments (ignore it as a beginner's mistake if it is one). I haven't found any other resource to fix the bug of handling an empty string scenario. Any help would be appreciated.
I tried doing
WITH input_students as (select *
from json_to_recordset(json_data :: json -> 'users')
as student (organization_id bigint, section text, class text,admission_number text))
insert into students select nullif(a.organization_id,''),a.section,a.class ,a.admission_number,'1',2,3,'true' from input_students a;
but it provides with an error
"error": "org.postgresql.util.PSQLException: ERROR: invalid input syntax for integer: \"\"\n Where: PL/pgSQL function hsg_put_user_class_section_v2(text) line 22 at SQL statement"
The function is :
create or replace function hsg_put_user_class_section_v2(json_data text)
returns table(org_id bigint,
admission_number text,
status text)
LANGUAGE plpgsql
AS $function$
declare
error_organization_id_invalid character varying := 'Organization ID is invalid !';
error_admission_number_invalid character varying := 'Admission Number is invalid !';
error_user_does_not_exist character varying := 'User does not exist !';
error_class_not_found character varying := 'Class not found !';
error_section_not_found character varying := 'Section not found';
success_message character varying :='Updated Successfully';
begin
CREATE TEMP TABLE students (
organization_id bigint,
section text,
class text,
admission_number text,
admission_number_in_hsg text,
organization_id_in_hsg bigint,
user_id bigint,
status text
)
ON COMMIT DROP;
WITH input_students as (select *
from json_to_recordset(json_data :: json -> 'users')
as student (organization_id bigint, section text, class text,admission_number text))
insert into students select nullif(a.organization_id,0),a.section,a.class ,a.admission_number,'1',2,3,'true' from input_students a;
return query select
s.organization_id,
s.admission_number,
s.status
from students s;
end;
$function$ ;
stundentslikereturn query SELECT * FROM students