0

ON PostgreSQL 9.6.10 (Red Hat 4.8.5-28), 64-bit

select string_to_array('file.name.pdf', '.')
{file,nome,pdf}

select array_length(string_to_array('file.nome.pdf', '.'))
[42883]: ERROR: function array_length(text[]) does not exist

SELECT length(string_to_array('file.nome.pdf', '.'));
[42883]: ERROR: function length(text[]) does not exist

I would like return 3 like result of length of array of elements. I would like to use it into function to manage fileName.

elements = string_to_array('file.name.pdf', '.');
extention = elements[array_length(elements)]; -- LAST single extension es: doc, pdf, svg, html, ...
file_name = STRING_AGG(elements[:array_length(elements-1)]; -- "file.name" for "file.name.pdf"
4
  • What makes you think that array_length exists? Commented Jul 19, 2024 at 17:49
  • You can't store a PDF in a text field - it doesn't handle storing non-character data. But have you simply tried the length() function? Commented Jul 19, 2024 at 18:14
  • @ScottHunter: It does, so… Commented Jul 20, 2024 at 7:14
  • @stdunbar: Can you store a PDF’s file name in a text field? Commented Jul 20, 2024 at 7:14

1 Answer 1

0

As documented in Array Functions and Operators, you need to pass a dimension index to array_length, even if your array is one-dimensional.

select array_length(string_to_array('file.nome.pdf', '.'), 1)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.