CREATE EXTERNAL FILE FORMAT#

Creates an external file format in the current virtual database: a named, reusable description of how CREATE EXTERNAL TABLE ... AS SELECT writes its files - the format type, the delimiters, the header row, the encoding, how dates are rendered and how the files are compressed. The statement takes every clause and option SQL Server 2025 takes, with the same limits and error numbers, and extends two option value sets.

Syntax#

CREATE EXTERNAL FILE FORMAT format name WITH ( FORMAT_TYPE = DELIMITEDTEXT PARQUET ORC DELTA , FORMAT_OPTIONS ( FIELD_TERMINATOR = 'terminator' STRING_DELIMITER = 'delimiter' FIRST_ROW = integer DATE_FORMAT = 'pattern or culture' USE_TYPE_DEFAULT = TRUE FALSE ENCODING = 'encoding' , ) , DATA_COMPRESSION = 'codec' )

Arguments#

format_name

The name of the format, unique within the virtual database (the comparison ignores letter case). A name that exists raises error 46502. The names of the built-in formats - csv, tsv and parquet - are reserved in every database and raise the same error.

FORMAT_TYPE = { DELIMITEDTEXT | PARQUET | ORC | DELTA }

Required. The type is written bare, in any letter case, and stored as typed. RCFILE parses but is refused at execution with error 46506, as SQL Server refuses it; an ORC or DELTA format can be created, but no export writes it. A statement without FORMAT_TYPE is refused with error 46505.

FORMAT_OPTIONS ( … )

Accepted for DELIMITEDTEXT only; naming any option for another type raises error 46506. An option may be repeated, and FORMAT_OPTIONS may be repeated or empty - every occurrence is validated and the last one is stored, as SQL Server does.

FIELD_TERMINATOR = 'terminator'

Up to 10 characters (error 46508 beyond). When omitted, the stored value is |.

STRING_DELIMITER = 'delimiter'

Up to 10 characters (error 46508 beyond). When omitted, text is not quoted.

FIRST_ROW = integer

An integer from 1 to 101 (error 46529 outside that range; a sign, a decimal point or a string is a syntax error). When omitted, the stored value is 1.

DATE_FORMAT = 'pattern or culture'

Up to 50 characters (error 46508 beyond). The value is either a culture name (pl-PL, en-US, any predefined culture, matched without regard to letter case) or a .NET custom date and time pattern (dd.MM.yyyy HH:mm), never both; the empty string means “not set”. A pattern that cannot format a date and time is refused when the format is created, and so is a pattern that writes a time-zone offset (z, zz, zzz or K outside a quoted literal, or the standard o, O and U): it applies to every temporal type, and a datetime2 or time value has no offset to write, so the server’s own offset would appear instead. A culture already writes the offset of a datetimeoffset value. A culture whose decimal separator or date separator occurs in the field terminator is refused as well - a pl-PL format with a , terminator would split every number. Each of these refusals is error 46508. The culture and pattern forms are extensions of SQL Server, which stores the value and ignores it.

USE_TYPE_DEFAULT = { TRUE | FALSE }

Accepted and stored, as SQL Server does.

ENCODING = 'encoding'

'UTF8' and 'UTF16', SQL Server’s two values, spelled exactly; and, as extensions, 'UTF8BOM' (UTF-8 with a byte-order mark, which spreadsheet applications need) and a code page given as a number of at most 10 digits, from 1 to 65535 ('1250', '65001'), for which the server has an encoding. Any other value, 'utf8', 'windows-1250' and a number that names no encoding ('12345') included, is refused with error 46508. When omitted, the stored value is UTF8.

DATA_COMPRESSION = ‘codec’

One of the three codec names SQL Server knows, spelled exactly: org.apache.hadoop.io.compress.DefaultCodec, org.apache.hadoop.io.compress.GzipCodec and org.apache.hadoop.io.compress.SnappyCodec; any other spelling is a syntax error (46508). Whether the format type takes the codec is checked when the statement runs, exactly as SQL Server checks it (error 46506 otherwise): DELIMITEDTEXT takes Gzip; PARQUET and DELTA take Snappy and Gzip; ORC takes Default and Snappy. May be repeated; every occurrence is checked and the last one is stored.

Remarks#

A format is a database-scoped object: it belongs to one virtual database, its file_format_id starts at 65536 and is never reused, it is removed with the database, and a clone of the database carries it. It cannot be created in the system databases (master, msdb).

Every option is stored as declared; sys.external_file_formats lists the database’s user-defined formats and fills in the defaults SQL Server reports for the options a declaration omits. The built-in formats are not listed there.

The three clauses may come in any order, and each may be repeated; the statement is what SQL Server’s own client tools (SSMS, SSDT, SqlPackage) parse, so a database project can declare a format and deploy it. The two extended option value sets - ENCODING beyond UTF8/UTF16 and DATE_FORMAT as a culture or pattern - are new values of existing options, so those tools accept them too; SQL Server itself refuses them with error 46508.

Deploying a database project to Querona with SqlPackage is not supported today: the tool’s deployment script and its read of the database use statements Querona does not yet accept, so a publish stops before it creates any object and an extract fails. Run the project’s CREATE EXTERNAL FILE FORMAT statements as a script instead. Against SQL Server 2025 the same tools drop and recreate, on every publish, a format that declares ENCODING or FIRST_ROW and every DELTA format, and an extract loses those options - a defect in the tool’s SQL Server 2025 reader, not in the server.

What an export writes through the format#

A CREATE EXTERNAL TABLE ... AS SELECT over a format you defined writes what SQL Server 2025 writes for the same declaration, byte for byte, with the two extended options on top:

  • Rows and fields. Rows end with a line feed. Fields are separated by FIELD_TERMINATOR, whose backslash escapes are resolved when the file is written (\t, \n, \r, \\; \0 produces nothing); an empty terminator writes the fields with nothing between them. NULL is written as nothing.

  • Quoting. Only char, varchar, nchar and nvarchar columns are wrapped in STRING_DELIMITER, and every occurrence of the delimiter inside a value is doubled; an empty string becomes the delimiter twice. Without STRING_DELIMITER nothing is quoted, so a value holding the terminator or a line break is written raw.

  • Header. FIRST_ROW = 2 writes a header row of the column names; a larger value adds one line reading This is auto generated header to be consistent across import/export. per row beyond the second. Header names are written raw, never quoted. An empty result still writes the file (SQL Server writes none).

  • Values. Integers and bit as digits; decimal at its declared scale; money with four decimals; float and real with seventeen significant digits in exponent form (1.0000000000000001e-001); binary as upper-case hexadecimal without 0x; uniqueidentifier in upper case; date as yyyy-MM-dd, the other temporal types as yyyy-MM-dd HH:mm:ss with the fraction at the column’s scale, datetimeoffset followed by its offset. The types SQL Server refuses - xml, sql_variant, geometry, rowversion, text, ntext, image - are refused with error 15877 before any file is written; cast them in the SELECT.

  • Encoding. The declared encoding, with its byte-order mark: none for UTF8 and the numeric code pages, FF FE for UTF16, EF BB BF for UTF8BOM. A code page encodes strictly: a character it cannot hold fails the export, names the column and the character, and leaves no file.

  • ``DATE_FORMAT``. A pattern is applied to every temporal type; a culture renders dates in its short date pattern, times in its long time pattern with the fraction after the seconds, and decimals and floats with its decimal separator, always in the Gregorian calendar with ASCII digits. The text a pattern or culture produces is quoted like a text value and escaped against spreadsheet formulas; without STRING_DELIMITER such text may not contain the terminator or a line break. Culture data comes from the operating system’s ICU library, so the exact output can change with an operating system update.

  • Spreadsheet formula escaping. A text value or column name starting with =, +, -, @, a tab or a carriage return is prefixed with an apostrophe when the format uses an extension - UTF8BOM, a code page, or a culture in DATE_FORMAT - and written raw, as SQL Server writes it, otherwise.

  • Parquet. DATA_COMPRESSION selects the codec: omitted means uncompressed, Snappy and Gzip are honoured. An ORC or DELTA format can be created, but an export over it is refused with error 11560.

The built-in formats keep their own output: csv and tsv quote by the RFC 4180 rules, end rows with CRLF, always write a header and escape formulas, and write dates as yyyy-MM-ddTHH:mm:ss.fffffff; parquet compresses with Snappy.

Permissions#

Requires ALTER ANY EXTERNAL FILE FORMAT in the current database, which ALTER and CONTROL on the database imply - see Access rights.

Examples#

A minimal delimited-text format, stored with SQL Server’s defaults (| terminator, no quoting, UTF-8):

CREATE EXTERNAL FILE FORMAT pipe_text WITH (FORMAT_TYPE = DELIMITEDTEXT);

A comma-separated, quoted, UTF-8-with-BOM format with a header row - the shape a spreadsheet opens by double-click:

CREATE EXTERNAL FILE FORMAT excel_csv
WITH (
    FORMAT_TYPE = DELIMITEDTEXT,
    FORMAT_OPTIONS (FIELD_TERMINATOR = ',', STRING_DELIMITER = '"', FIRST_ROW = 2, ENCODING = 'UTF8BOM')
);

A semicolon-separated format whose dates and decimals follow the Polish locale:

CREATE EXTERNAL FILE FORMAT polish_csv
WITH (
    FORMAT_TYPE = DELIMITEDTEXT,
    FORMAT_OPTIONS (FIELD_TERMINATOR = ';', DATE_FORMAT = 'pl-PL', ENCODING = 'UTF8BOM')
);

A Gzip-compressed Parquet format:

CREATE EXTERNAL FILE FORMAT gzip_parquet
WITH (FORMAT_TYPE = PARQUET, DATA_COMPRESSION = 'org.apache.hadoop.io.compress.GzipCodec');

See also#