Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7869551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:21:36+00:00 2026-06-03T01:21:36+00:00

I am trying to export from SQL to .csv and it works if I

  • 0

I am trying to export from SQL to .csv and it works if I hard code it to accept a certain number of arguments. The things is, I want to allow the user to request any number of arguments and have these be passed to the where clause. The code should make this a bit more clear.

create temporary table bdates as

 select tt.date, tt.time, tt.location
 from birthdays as bd
 inner join days as d
   on (d.id = bd.birth_id)
 inner join total_time as tt
   on (bd.date = tt.date and
       bd.time = tt.time and
       d.day_of = tt.location)
 where tt.date in(:date1, :date2) --defined by user at command line
 order by...

\copy bdates to '.csv'

So what I think I want to do is pass a list to that where clause instead of explicit :dates# variables. For example, a person could run the script with the argument ‘2012-01-04 12:00, 2012-02-04 12:00, 2012-03-04 12:00’, or just two arguments or one. In the case of three the string would be parsed to ‘2012-01-04 12:00’, ‘2012-02-04 12:00’, ‘2012-03-04 12:00’.

I’ve tried string_to_array(), unnest(regexp_matches(:dates, expression)) and regexp_split_to_table(:dates, expression), though I’m not sure how to do the join. the various solutions I’ve tried have produced numerous errors including:

cannot cast type text[] to timestamp without time zone

cannot cast type record to timestamp without time zone

regexp_split does not support the global option

argument of WHERE must not return a set

The last one is especially disheartening and I’m at a loss and would appreciate any input. There’s a simpler way to do this, isn’t there? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:21:37+00:00Added an answer on June 3, 2026 at 1:21 am

    Try this:

    create table x(d timestamp);
    
    insert into x values
    ('jan 2, 2012'),
    ('february 4, 2012 12:00'),
    ('jan 4, 2012 12:00'),
    ('march 1, 2012'),
    ('may 3, 2012');
    

    Query:

    with input as
    (
      select 
      '2012-1-2, 2012-01-04 12:00, 2012-02-04 12:00, 2012-03-04 12:00'::text
      as d_input
    )
    ,converted_to_array as
    (
      select ('{' || d_input || '}')::timestamp[] as d_array
      from input 
    )
    select d
    from x cross join converted_to_array
    where d = any(d_array)
    

    Output:

    D
    January, 02 2012 00:00:00-0800
    February, 04 2012 12:00:00-0800
    January, 04 2012 12:00:00-0800
    

    Live test: http://www.sqlfiddle.com/#!1/43d48/26


    You can also use IN, just unnest array to rows:

    with input as
    (
      select 
      '2012-1-2, 2012-01-04 12:00, 2012-02-04 12:00, 2012-03-04 12:00'::text
      as d_input
    )
    ,converted_to_array as
    (
      select ('{' || d_input || '}')::timestamp[] as d_array
      from input 
    )
    select d
    from x cross join converted_to_array
    where d in (select unnest(d_array))
    

    Live test: http://www.sqlfiddle.com/#!1/43d48/29


    You can put them all in one line too:

    select d
    from x 
    where d in (select unnest( ('{' || '2012-1-2, 2012-01-04 12:00, 2012-02-04 12:00, 2012-03-04 12:00'::text || '}')::timestamp[] ))
    

    But I hesitates to do so, as it causes horizontal scrollbar here on stackoverflow 🙂

    Live test: http://www.sqlfiddle.com/#!1/43d48/31

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to bulk export out of a sql table, the code works
I'm trying to export data from SQL Server into CSV format. I have a
I'm trying to export records from SQL Server 2008 to mdb file using OpenDataSource.
I am trying to export the following data in the query from ms sql
I'm trying to export a copy of the Explain Plan from Oracle SQL Developer
I am trying to export some data from a SQL Express table to Access
I've got this code I'm trying to use to export data from Excel to
I'm trying to export from SQL Server to Oracle a table that consists of
I'm trying to dump data from a SQL export file with regular expression. To
I'm trying to export a profile from WAS 6.1 so that I can give

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.