1

I created a table has a column store text data. I want to get specified text value from the column. I want to get PAYMENTDATE value. How can I do?

"{"SALEDATE":"2017-11-01 12:46:29","PAYMENTDATE":"2017-11-01 12:50:49"}"
4
  • 1
    why dont use a JSON column instead? postgresql.org/docs/9.4/static/datatype-json.html Commented Dec 1, 2017 at 14:40
  • You are right. I know JSON type. But I have to use text data. Commented Dec 1, 2017 at 14:42
  • 1
    You still can cast the text field as JSON and work with it. text_data::JSON Commented Dec 1, 2017 at 14:43
  • I solved such as thanks. columnName::json->>'PAYMENTDATE' as paymentDate Commented Dec 1, 2017 at 14:43

1 Answer 1

1

If you trim the leading and trailing " from the string, you'll have a json-formatted string. You could then just cast it to json and use the ->> operator to extract data from it:

SELECT TRIM(BOTH '"' FROM mycol)::JSON->>'PAYMENTDATE'
FROM   mytable
Sign up to request clarification or add additional context in comments.

3 Comments

My guess those " are the result from printing the field content.
columnName::json->>'PAYMENTDATE' as paymentDate. This is worked thanks.
@JuanCarlosOropeza so it's even easier then - just lose the trim call

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.