0

Ok I am very new to postgres so bear with me. I have been using the string_agg function in postgres to get my CSV. I want to know how to use my CSV in a query. I did this in SQL server by using a DECLARE but as I have read there is not a way to DECLARE globally in postgres. Any help, documentation, examples would be greatly appreciated.

3
  • To be honest i don't know what you're trying to achive. I don't see correlation between string_agg and DECLARE. Do you want to create CSV? or process given file? or do you just look for DECLARE equivalent? What have you done yet and what's your particular problem? Commented Oct 9, 2015 at 13:24
  • I guess I should clarify. I want to use string_agg like a CSV (Comma Sperated Value). So when I use it I can get a listing of lets say recordIDs. Now I want to use the that list in a function. Commented Oct 9, 2015 at 13:28
  • A very simple example of this would be running a string_agg to get all recordIDs that are less than 100. Then write a query 'code' Select * From Table1 Where recordID(string_agg) Commented Oct 9, 2015 at 13:31

1 Answer 1

1

Ok, so based on your question and your comment. PostgreSQL has ARRAY type which fits nicely in SQL statements, so you can make an array from your CSV-string and use it in queries like:

SELECT * FROM table WHERE record_id = ANY(string_to_array( string_agg , ','));

Actually there is no need to use CSV at all. If you have such option change to use array_agg in the first place (instead of string_agg).

Function string_to_array(text, delimiter) splits string into array with delimiter passed as an argument. Expression record_id = ANY( array ) returns TRUE when "any" of the array elements is equal to record_id and FALSE otherwise.

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.