1

I am trying to execute the following query:

select
    d.ro_number
    ,d.ro_closed_date
    ,d.customer_name
    ,d.veh_year
    ,d.veh_make
    ,d.veh_model
    ,d.op_code_name
    ,r.vin
    ,r.customer_email
    ,r.customer_contact_phone
    , substr(r.customer_contact_phone, 1, 3) as contact_number
from public.declined_work d
    inner join public.ro_closed r
        on d.ro_number = r.ro_number

I am receiving the following error:

ERROR:  function substr(text[], integer, integer) does not exist
LINE 12:  , substr(r.customer_contact_phone, 1, 3) as contact_number
            ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 172

"customer_contact_phone" is data type "text."

I am relatively new to postgres, but to not to SQL in general. I am a bit baffled at what the issue is. I also tried the syntax as in this example

substring('Thomas' from 2 for 3)  

But that did not work either.

I tried changing the function to substring as I was told substr doesn't exist in postgres, but I got the same error when I executed this:

select
    d.ro_number
    ,d.ro_closed_date
    ,d.customer_name
    ,d.veh_year
    ,d.veh_make
    ,d.veh_model
    ,d.op_code_name
    ,r.vin
    ,r.customer_email
    ,r.customer_contact_phone
    ,substring(r.customer_contact_phone, 1, 3) as contact_number
from public.declined_work d
    inner join public.ro_closed r
        on d.ro_number = r.ro_number

I thought maybe it is an error due to the data type being text, but that doesn't make any sense to me. The values in that column look like this:

{7147019909, 5246627378}

Thanks in advance.

1 Answer 1

2

This works in Postgres:

substring(r.customer_contact_phone, 1, 3) as contact_number

Note that this uses substring() rather than substr() (the latter does not exist in Postgres).

If you want the first three characters, you can also use left():

left(r.customer_contact_phone, 3) as contact_number

The syntax you attempted to use also works:

substring(r.customer_contact_phone from 1 for 3) as contact_number
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, I got the same error with that select substring(r.customer_contact_phone, 1, 3) as contact_number from public.ro_closed r ERROR: function pg_catalog.substring(text[], integer, integer) does not exist LINE 1: select substring(r.customer_contact_phone, 1, 3) as contact_... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. SQL state: 42883 Character: 8
@Travis: it appears that your column is an array of texts, not just a text (that's what text[] stands for). What are you actually trying to do?
Yeah that's accurate, it's phone numbers and sometimes there is more than one. I am trying to strip off the brackets, the comma and any numbers after the first one that appears.
I was able to figure it out, thank you. I was not aware I was looking at an array of text, not a text field. I used ARRAY_TO_STRING and was able to do the transformation I needed. Thanks!

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.