BCP Error - copy direction must be in, out or format

Padmanabhan, Venkatesh 241 Reputation points
2021-03-29T04:49:33.657+00:00

Hi.

I am trying to use the BCP in command line. I am trying for the below code:

bcp "select ('"' + replace(ltrim(rtrim(diary)), '""', '""""') + '"') as diry from test.txt " queryout "path" -t -s servername -d dbname -b 500 -c -c 65001 -t~

I am getting error as - copy direction must be in, out or format

How to fix this

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,692 questions
{count} votes

Accepted answer
  1. Erland Sommarskog 100.9K Reputation points MVP
    2021-03-31T21:37:39.62+00:00

    This is starting to get pretty ugly. We need replace CR+LF with nothing (or would you prefer spaces?). But try this:

    bcp "select char(34) + replace(replace(replace(ltrim(rtrim(diary)), replicate(char(34), 2), replicate(char(34), 4)), char(13), ''), char(10), '') + char(34) as diry from test.txt"    queryout "path" -T -S servername -d dbname -b 500 -c -C 65001 -t~
    

    And because of the command-line syntax, it has to be a single line.


2 additional answers

Sort by: Most helpful
  1. Viorel 112.1K Reputation points
    2021-03-29T12:48:38.647+00:00

    Try this command:

    bcp "select ('""' + replace(ltrim(rtrim(diary)), '""', '""""') + '""') as...


  2. Erland Sommarskog 100.9K Reputation points MVP
    2021-03-29T22:05:25.837+00:00

    Try this:

    bcp "select (char(34) + replace(ltrim(rtrim(diary)), replicate(char(34), 2), replicate(char(34), 4) + char(34)) as diry from test.txt " queryout "path" -T -S servername -d dbname -b 500 -c -C 65001 -t~
    

    I have also corrected three switches that incorrectly were lowercase. BCP comes from the Unix world, and the switches are case-sensitive.