I’ve been trying to insert a case statement into my create function statement and haven’t had any luck. Just a heads up I am using pgadmin and postgresql. I have a table with rental movie titles, along with an integer that identifies what genre that movie falls in. I am grabbing the genre integer/ category_id from another table and wanted to transform the column data into a String representing the genre name instead of an id number for readibility. I must use a CREATE FUNCTION to transform the column. The code i came up with is below. I am getting the following error:
ERROR: syntax error at end of input LINE 14: $$
^
CREATE OR REPLACE FUNCTION idtoword(category_id INTEGER)
RETURNS VARCHAR(40)
LANGUAGE plpgsql
AS
$$
DECLARE genreWord VARCHAR(40);
BEGIN
SELECT CASE category_id
WHEN 12 THEN 'COMEDY'
WHEN 1 THEN 'DRAMA'
END
INTO genre
END;
$$
I tried multiple ways to insert the case statement into the create function. Something is wrong with my syntax. I would like the function to take a column of category id’s and return the actual genre name I specify in the case statement
2
Answers
Within a PostgreSQL function, try this code to transform a category ID into its corresponding genre word using a CASE statement.
You haven’t added the correct syntax of words as:
This you have missed as well I have added else unknown here.
You need to add Into genreWord and same as return genreWord
I hope this will help you to resolve the error!!