I have a string array that represents the days of the week, but I want to map it to an integer array.
I have created a function to do it, but it always returns null, I am sure I am missing something obvious but I cant see it.
create or replace function days_textArray_toIntArray(daysArray text[])
returns int[]
language plpgsql
set search_path = ''
as $$
declare
textArray text[];
intArray int[];
begin
if ('Sun' = ANY(textArray)) then
intArray = array_append(intArray,0);
elseif ('Mon' = ANY(textArray)) then
intArray = array_append(intArray,1);
elseif ('Tue' = ANY(textArray)) then
intArray = array_append(intArray,2);
elseif ('Wed' = ANY(textArray)) then
intArray = array_append(intArray,3);
elseif ('Thu' = ANY(textArray)) then
intArray = array_append(intArray,4);
elseif ('Fri' = ANY(textArray)) then
intArray = array_append(intArray,5);
elseif ('Sat' = ANY(textArray)) then
intArray = array_append(intArray,6);
end if;
return intArray;
end;
$$
when I invoke it: select days_textArray_toIntArray(‘{Wed,Sun}’::text[]) I just get a null value returned when I would expect to get an array with [3,0] that corresponds to the text array entered.
2
Answers
Your function is returning
null
because you do not assign any value to the textArray variable, Therefore it remains as an empty array. Therefore, none of your conditions inside the if block will be true, and it will return an empty array.You might need to assign the input parameter
daysArray
totextArray
in your function.Refer the below code for reference:
You do not need a function for this; it can be done with a single SQL statement.
How it works: It begins Unnesting the the array of names and standardizing to lower case and 3character length. It them extracts the position within the full array of day of week names (in desired order) and subtracting 1, since arrays in Postgres are 1-based indexes. Finally, it aggregates those results back into an array.
Reference: Postgres array functions.
If this is commonly used in your app the it can be defined as aSQL function. See Demo here: