2

My query is the following:

SELECT id, category FROM table1

This returns the following rows:

ID|category
1 |{IN, SP}
2 | 
3 |{VO}

Does anyone know how i can remove the first char and last char of the string in PostgreSQL, so it removes: {}?

2
  • Can these two characters show up somewhere else? (And supposed to be kept?) Commented Jan 4, 2017 at 10:13
  • No. I want to retrieve the String in my select statement without {} but it's a foreign column containing an array Commented Jan 4, 2017 at 10:15

7 Answers 7

5

Not sure, what you mean with "foreign column", but as the column is an array, the best way to deal with that is to use array_to_string()

SELECT id, array_to_string(category, ',') as category
FROM table1;

The curly braces are not part of the stored value. This is just the string representation of an array that is used to display it.

Sign up to request clarification or add additional context in comments.

Comments

2

Either using multiple REPLACE functions.

SELECT id, REPLACE(REPLACE(category, '{', ''), '}', '') 
FROM table1

Or using a combination of the SUBSTRING, LEFT & LENGTH functions

SELECT id, LEFT(SUBSTRING(category, 2, 999),LENGTH(SUBSTRING(category, 2, 999)) - 1)
FROM table1

Or just SUBSTRING and LENGTH

SELECT id, SUBSTRING(category, 2, LENGTH(category)-2)
FROM table1

1 Comment

len() is not a PostgreSQL function (neither is an ANSI SQL one). You could use length(), char_length() or character_length()
1

You could replace the {} with an empty string

SELECT id, replace(replace(category, '{', ''), '}', '') FROM table1

Comments

0
select id,
      substring(category,charindex('{',category,len(category))+2,len(category)-2) 
from table1;

1 Comment

There is no charindex() or len() function in Postgres.
0
select id
      ,left(right(category,length(category)-1),length(category)-2) category 
from boo

select id
       ,trim(both '{}' from category) 
from boo 

Trim():

Remove the longest string containing only the characters (a space by default) from the start/end/both ends of the string

Comments

-1

The syntax for the replace function in PostgreSQL is:

replace( string, from_substring, to_substring )

Parameters or Arguments

string
The source string.

from_substring
The substring to find. All occurrences of from_substring found within string are replaced with to_substring.
to_substring
The replacement substring. All occurrences of from_substring found within string are replaced with to_substring.

Comments

-1
UPDATE dbo.table1
SET category = REPLACE(category, '{', '')
WHERE ID <=3

UPDATE dbo.table1
SET category = REPLACE(category, '}', '')
WHERE ID <=3

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.