Skip to content

Latest commit

 

History

History
223 lines (152 loc) · 11.6 KB

File metadata and controls

223 lines (152 loc) · 11.6 KB
title Use Character Format to Import & Export Data
description Character format uses character data format for all columns. This is useful working with other programs or copying to an instance from another database vendor.
author rwestMSFT
ms.author randolphwest
ms.date 05/27/2025
ms.service sql
ms.subservice data-movement
ms.topic concept-article
helpviewer_keywords
data formats [SQL Server], character
character formats [SQL Server]
monikerRange >=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric

Use character format to import or export data (SQL Server)

[!INCLUDE SQL Server Azure SQL Database Synapse Analytics PDW FabricDW]

Character format is recommended when you bulk export data to a text file that is to be used in another program or when you bulk import data from a text file that is generated by another program.

Character format uses the character data format for all columns. Storing information in character format is useful when the data is used with another program, such as a spreadsheet, or when the data needs to be copied into an instance of [!INCLUDE ssNoVersion] from another database vendor such as Oracle.

Note

When you bulk transfer data between instances of [!INCLUDE ssNoVersion] and the data file contains Unicode character data but not any extended or DBCS characters, use the Unicode character format. For more information, see Use Unicode character format to import or export data (SQL Server).

Considerations for using character format

When using character format, consider:

  • By default, the bcp utility separates the character-data fields with the tab character and terminates the records with the newline character. For information about how to specify alternative terminators, see Specify field and row terminators (SQL Server).

  • By default, before the bulk export or import of character-mode data, the following conversions are performed:

    Direction of bulk operation Conversion
    Export Converts data to character representation. If explicitly requested, the data is converted to the requested code page for character columns. If no code page is specified, the character data is converted by using the OEM code page of the client computer.
    Import Converts character data to native representation, when necessary, and translates the character data from the client's code page to the code page of one or more target columns.
  • To prevent loss of extended characters during conversion, either use Unicode character format or specify a code page.

  • Any sql_variant data that is stored in a character-format file is stored without metadata. Each data value is converted to char format, according to the rules of implicit data conversion. When imported into a sql_variant column, the data is imported as char. When imported into a column with a data type other than sql_variant, the data is converted from char by using implicit conversion. For more information about data conversion, see Data type conversion (Database Engine).

  • The bcp utility exports money values as character-format data files with four digits after the decimal point and without any digit-grouping symbols such as comma separators. For example, a money column that contains the value 1,234,567.123456 is bulk exported to a data file as the character string 1234567.1235.

Command options for character format

You can import character format data into a table using bcp, BULK INSERT (Transact-SQL), or OPENROWSET (BULK). For a bcp command or BULK INSERT (Transact-SQL) statement, you can specify the data format in the statement. For an OPENROWSET (BULK) statement, you must specify the data format in a format file.

Character format is supported by the following command options:

Command Option Description
bcp -c Causes the bcp utility to use character data. 1
BULK INSERT DATAFILETYPE ='char' Use character format when bulk importing data.
OPENROWSET N/A Must use a format file

1 To load character (-c) data to a format compatible with earlier versions of [!INCLUDE ssNoVersion] clients, use the -V switch. For more information, see Import native and character format data from earlier versions of SQL Server.

Note

Alternatively, you can specify formatting on a per-field basis in a format file. For more information, see Format files to import or export data (SQL Server).

Example test conditions

The examples in this article are based on the following table and format file.

Sample table

The following script creates a test database, a table named myChar and populates the table with some initial values. Execute the following Transact-SQL in Microsoft [!INCLUDE ssManStudioFull] (SSMS):

CREATE DATABASE TestDatabase;
GO

USE TestDatabase;

CREATE TABLE dbo.myChar
(
    PersonID SMALLINT NOT NULL,
    FirstName VARCHAR (25) NOT NULL,
    LastName VARCHAR (30) NOT NULL,
    BirthDate DATE,
    AnnualSalary MONEY
);

-- Populate table
INSERT TestDatabase.dbo.myChar
VALUES (1, 'Anthony', 'Grosse', '1980-02-23', 65000.00),
       (2, 'Alica', 'Fatnowna', '1963-11-14', 45000.00),
       (3, 'Stella', 'Rossenhain', '1992-03-02', 120000.00);

-- Review data
SELECT * FROM TestDatabase.dbo.myChar;

Sample non-XML format file

SQL Server support two types of format file: non-XML format and XML format. The non-XML format is the original format supported by earlier versions of SQL Server. For more information, see Use Non-XML format files (SQL Server). The following command uses the bcp utility to generate a non-XML format file, myChar.fmt, based on the schema of myChar. To use a bcp command to create a format file, specify the FORMAT argument and use nul instead of a data-file path. The format option also requires the -f option. In addition, for this example, the qualifier c is used to specify character data, and T is used to specify a trusted connection using integrated security.

At a command prompt, enter the following command:

bcp TestDatabase.dbo.myChar format nul -f D:\BCP\myChar.fmt -T -c

REM Review file
Notepad D:\BCP\myChar.fmt

Important

Ensure your non-XML format file ends with a carriage return\line feed. Otherwise you might receive the following error message:

SQLState = S1000, NativeError = 0
Error = [Microsoft][ODBC Driver 13 for SQL Server]I/O error while reading BCP format file

Examples

The following examples use the database and format files created previously in this article.

Use bcp and character format to export data

-c switch and OUT command.

The data file created in this example is used in all subsequent examples.

At a command prompt, enter the following command:

bcp TestDatabase.dbo.myChar OUT D:\BCP\myChar.bcp -T -c

REM Review results
NOTEPAD D:\BCP\myChar.bcp

Use bcp and character format to import data without a format file

The -c switch and IN command. At a command prompt, enter the following command:

REM Truncate table (for testing)
SQLCMD -Q "TRUNCATE TABLE TestDatabase.dbo.myChar;"

REM Import data
bcp TestDatabase.dbo.myChar IN D:\BCP\myChar.bcp -T -c

REM Review results
SQLCMD -Q "SELECT * FROM TestDatabase.dbo.myChar;"

Use bcp and character format to import data with a non-XML format file

The -c and -f switches and IN command. At a command prompt, enter the following command:

REM Truncate table (for testing)
SQLCMD -Q "TRUNCATE TABLE TestDatabase.dbo.myChar;"

REM Import data
bcp TestDatabase.dbo.myChar IN D:\BCP\myChar.bcp -f D:\BCP\myChar.fmt -T

REM Review results
SQLCMD -Q "SELECT * FROM TestDatabase.dbo.myChar;"

Use BULK INSERT and character format without a format file

The DATAFILETYPE argument. Execute the following Transact-SQL in Microsoft [!INCLUDE ssManStudioFull] (SSMS):

TRUNCATE TABLE TestDatabase.dbo.myChar; -- for testing

BULK INSERT TestDatabase.dbo.myChar FROM 'D:\BCP\myChar.bcp'
    WITH (DATAFILETYPE = 'Char');

-- review results
SELECT * FROM TestDatabase.dbo.myChar;

Use BULK INSERT and character format with a non-XML format file

This is an example of the FORMATFILE argument. Execute the following Transact-SQL in Microsoft [!INCLUDE ssManStudioFull] (SSMS):

TRUNCATE TABLE TestDatabase.dbo.myChar; -- for testing

BULK INSERT TestDatabase.dbo.myChar FROM 'D:\BCP\myChar.bcp'
    WITH (FORMATFILE = 'D:\BCP\myChar.fmt');

-- review results
SELECT * FROM TestDatabase.dbo.myChar;

Use OPENROWSET and character format with a non-XML format file

An example of the FORMATFILE argument. Execute the following Transact-SQL in Microsoft [!INCLUDE ssManStudioFull] (SSMS):

TRUNCATE TABLE TestDatabase.dbo.myChar; -- for testing

INSERT INTO TestDatabase.dbo.myChar
SELECT * FROM OPENROWSET (
    BULK 'D:\BCP\myChar.bcp',
    FORMATFILE = 'D:\BCP\myChar.fmt'
) AS t1;

-- review results
SELECT * FROM TestDatabase.dbo.myChar;

Related tasks

To use data formats for bulk import or bulk export:

Related content