You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ODBC is the primary native data access API for applications written in C and C++ for SQL Server. There's an ODBC driver for most data sources. Other languages that can use ODBC include COBOL, Perl, PHP, and Python. ODBC is widely used in data integration scenarios.
18
19
19
-
The ODBC driver comes with tools such as [**sqlcmd**](../../tools/sqlcmd/sqlcmd-utility.md) and [**bcp**](../../tools/bcp-utility.md). The **sqlcmd** utility lets you run Transact-SQL statements, system procedures, and SQL scripts. The **bcp** utility bulk copies data between an instance of Microsoft SQL Server and a data file in a format you choose. You can use **bcp** to import many new rows into SQL Server tables or to export data out of tables into data files.
20
+
The ODBC driver comes with tools such as [**sqlcmd**](../../tools/sqlcmd/sqlcmd-utility.md) and [**bcp**](../../tools/bcp-utility.md). The **sqlcmd** utility lets you run Transact-SQL statements, system procedures, and SQL scripts. The **bcp** utility bulk copies data between an instance of Microsoft SQL Server and a data file in a format you choose. You can use **bcp** to import many new rows into SQL Server tables or to export data out of tables into data files.
20
21
21
22
## Code example in C++
22
23
@@ -71,4 +72,4 @@ The following sample demonstrates how to use the ODBC APIs to connect to and acc
71
72
72
73
## Community
73
74
74
-
-[SQL Server Drivers blog](https://techcommunity.microsoft.com/t5/SQL-Server/bg-p/SQLServer/label-name/SQLServerDrivers)
75
+
-[SQL Server Drivers blog](https://techcommunity.microsoft.com/t5/SQL-Server/bg-p/SQLServer/label-name/SQLServerDrivers)
Copy file name to clipboardExpand all lines: docs/relational-databases/import-export/specify-field-length-by-using-bcp-sql-server.md
+98-91Lines changed: 98 additions & 91 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ title: "Specify Field Length by Using bcp (SQL Server)"
3
3
description: In SQL Server, if necessary, bcp prompts for field length, default field lengths, and impact of field-length on data storage in files that contain char data.
[!INCLUDE[SQL Server Azure SQL Database Synapse Analytics PDW](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw.md)]
21
-
The field length indicates the maximum number of characters required to represent data in character format. If you store data in native format, the field length is already known. For example, the **int** data type takes 4 bytes. When you indicate 0 for the prefix length, the **bcp** command prompts you for the field length, default field lengths, and the impact of field length on data storage in data files that contain **char** data.
22
-
23
-
## The bcp Prompt for Field Length
20
+
[!INCLUDE [SQL Server Azure SQL Database Synapse Analytics PDW](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw.md)]
24
21
25
-
If an interactive **bcp** command contains the **in** or **out** option without either the format file switch (**-f**) or a data-format switch (**-n**, **-c**, **-w**, or **-N**), the command prompts for the field length of each data field, as follows:
26
-
27
-
`Enter length of field <field_name> [<default>]:`
28
-
29
-
For an example that shows this prompt in context, see [Specify Data Formats for Compatibility when Using bcp (SQL Server)](../../relational-databases/import-export/specify-data-formats-for-compatibility-when-using-bcp-sql-server.md).
22
+
The field length indicates the maximum number of characters that are required to represent data in character format. The field length is already known if the data is stored in the native format; for example, the **int** data type takes 4 bytes. If you have indicated 0 for the prefix length, the **bcp** command prompts you for field length, the default field lengths, and the impact of field-length on data storage in data files that contain **char** data.
30
23
31
-
> [!NOTE]
32
-
> After you interactively specify all of the fields in a **bcp** command, the command prompts you save your responses for each field in a non-XML format file. For more information on non-XML format files, see [Non-XML Format Files (SQL Server)](../../relational-databases/import-export/non-xml-format-files-sql-server.md).
24
+
## The bcp prompt for field length
25
+
26
+
If an interactive **bcp** command contains the `in` or `out` option without either the format file switch (`-f`) or a data-format switch (`-n`, `-c`, `-w`, or `-N`), the command prompts for the field length of each data field, as follows:
27
+
28
+
`Enter length of field <field_name> [<default>]:`
29
+
30
+
For an example that shows this prompt in context, see [Specify Data Formats for Compatibility when Using bcp (SQL Server)](../../relational-databases/import-export/specify-data-formats-for-compatibility-when-using-bcp-sql-server.md).
31
+
32
+
> [!NOTE]
33
+
> After you interactively specify all of the fields in a **bcp** command, the command prompts you save your responses for each field in a non-XML format file. For more information on non-XML format files, see [Non-XML Format Files (SQL Server)](../../relational-databases/import-export/non-xml-format-files-sql-server.md).
33
34
34
35
Whether a **bcp** command prompts for field length depends on several factors, as follows:
35
36
36
-
- When you copy data types that are not of fixed length and you specify a prefix length of 0, **bcp** prompts for a field length.
37
+
- When you copy data types that aren't of fixed length and you specify a prefix length of 0, **bcp** prompts for a field length.
37
38
38
39
- When converting noncharacter data to character data, **bcp** suggests a default field length large enough to store the data.
39
40
40
-
- If the file storage type is noncharacter, the **bcp** command does not prompt for a field length. The data is stored in the [!INCLUDE[msCoName](../../includes/msconame-md.md)][!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] native data representation (native format).
41
+
- If the file storage type is noncharacter, the **bcp** command doesn't prompt for a field length. The data is stored in the [!INCLUDE [msCoName](../../includes/msconame-md.md)][!INCLUDE [ssNoVersion](../../includes/ssnoversion-md.md)] native data representation (native format).
42
+
43
+
<aid="using-default-field-lengths"></a>
41
44
42
-
## Using Default Field Lengths
45
+
## Use default field lengths
43
46
44
-
Generally, [!INCLUDE[msCoName](../../includes/msconame-md.md)] recommends that you accept the **bcp**-suggested default values for the field length. When you create a character mode data file, using the default field length ensures that data is not truncated and that numeric overflow errors do not occur.
47
+
Generally, [!INCLUDE[msCoName](../../includes/msconame-md.md)] recommends that you accept the **bcp**-suggested default values for the field length. When a character mode data file is created, using the default field length ensures that data isn't truncated and that numeric overflow errors don't occur.
45
48
46
-
When you specify an incorrect field length, problems can occur. For instance, if you copy numeric data and specify a field length that is too short, the **bcp** utility prints an overflow message and does not copy the data. Also, if you export **datetime** data and specify a field length of less than 26 bytes for the character string, the **bcp** utility truncates the data without an error message.
49
+
If you specify a field length that is incorrect, problems can occur. For instance, if you copy numeric data and you specify a field length that is too short for the data, the **bcp** utility prints an overflow message and doesn't copy the data. Also, if you export **datetime** data and specify a field length of less than 26 bytes for the character string, the **bcp** utility truncates the data without an error message.
47
50
48
-
> [!IMPORTANT]
49
-
> When the default size option is used, [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] expects to read an entire string. In some situations, use of a default field length can lead to an "unexpected end of file" error. Typically, this error occurs with the **money** and **datetime** data types when only part of the expected field occurs in the data file; for example, when a **datetime** value of *mm*/*dd*/*yy* is specified without the time component and is, therefore, shorter than the expected 24 character length of a **datetime** value in **char** format. To avoid this type of error, use field terminators or fixed-length data fields, or change the default field length by specifying another value.
51
+
> [!IMPORTANT]
52
+
> When the default size option is used, [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] expects to read an entire string. In some situations, use of a default field length can lead to an "unexpected end of file" error. Typically, this error occurs with the **money** and **datetime** data types when only part of the expected field occurs in the data file; for example, when a **datetime** value of *mm*/*dd*/*yy* is specified without the time component and is, therefore, shorter than the expected 24 character length of a **datetime** value in **char** format. To avoid this type of error, use field terminators or fixed-length data fields, or change the default field length by specifying another value.
50
53
51
-
### Default Field Lengths for Character File Storage
54
+
### Default field lengths for character file storage
52
55
53
56
The following table lists the default field lengths for data to be stored as a character-file storage type. Nullable data is the same length as nonnull data.
|**nchar**|Twice the length defined for the column|
60
-
|**nvarchar**|Twice the length defined for the column|
61
-
|**Text**|0|
62
-
|**ntext**|0|
63
-
|**bit**|1|
64
-
|**binary**|Twice the length defined for the column + 1|
65
-
|**varbinary**|Twice the length defined for the column + 1|
66
-
|**image**|0|
67
-
|**datetime**|24|
68
-
|**smalldatetime**|24|
69
-
|**float**|30|
70
-
|**real**|30|
71
-
|**int**|12|
72
-
|**bigint**|19|
73
-
|**smallint**|7|
74
-
|**tinyint**|5|
75
-
|**money**|30|
76
-
|**smallmoney**|30|
77
-
|**decimal**|41*|
78
-
|**numeric**|41*|
79
-
|**uniqueidentifier**|37|
80
-
|**timestamp**|17|
81
-
|**varchar(max)**|0|
82
-
|**varbinary(max)**|0|
83
-
|**nvarchar(max)**|0|
84
-
|UDT|Length of the user-defined term (UDT) column|
85
-
|XML|0|
86
-
87
-
\*For more information about the **decimal** and **numeric** data types, see [decimal and numeric (Transact-SQL)](../../t-sql/data-types/decimal-and-numeric-transact-sql.md).
88
-
89
-
> [!NOTE]
58
+
| Data type | Default length (characters) |
59
+
| --- | --- |
60
+
|**char**| Length defined for the column |
61
+
|**varchar**| Length defined for the column |
62
+
|**nchar**| Twice the length defined for the column |
63
+
|**nvarchar**| Twice the length defined for the column |
64
+
|**Text**| 0 |
65
+
|**ntext**| 0 |
66
+
|**bit**| 1 |
67
+
|**binary**| Twice the length defined for the column + 1 |
68
+
|**varbinary**| Twice the length defined for the column + 1 |
69
+
|**image**| 0 |
70
+
|**datetime**| 24 |
71
+
|**smalldatetime**| 24 |
72
+
|**float**| 30 |
73
+
|**real**| 30 |
74
+
|**int**| 12 |
75
+
|**bigint**| 19 |
76
+
|**smallint**| 7 |
77
+
|**tinyint**| 5 |
78
+
|**money**| 30 |
79
+
|**smallmoney**| 30 |
80
+
|**decimal**| 41 <sup>1</sup> |
81
+
|**numeric**| 41 <sup>1</sup> |
82
+
|**uniqueidentifier**| 37 |
83
+
|**timestamp**| 17 |
84
+
|**varchar(max)**| 0 |
85
+
|**varbinary(max)**| 0 |
86
+
|**nvarchar(max)**| 0 |
87
+
|**UDT**| Length of the user-defined term (UDT) column |
88
+
|**XML**| 0 |
89
+
|**vector**| N/A <sup>2</sup> |
90
+
91
+
<sup>1</sup> For more information about the **decimal** and **numeric** data types, see [Decimal and numeric (Transact-SQL)](../../t-sql/data-types/decimal-and-numeric-transact-sql.md).
92
+
93
+
<sup>2</sup> Conversion of **vector** data type to and from character isn't allowed.
94
+
95
+
> [!NOTE]
90
96
> A column of type **tinyint** can have values from 0 through 255; the maximum number of characters that are needed to represent any number in that range is three (representing values 100 through 255).
91
97
92
-
### Default Field Lengths for Native File Storage
98
+
### Default field lengths for native file storage
93
99
94
100
The following table lists the default field lengths for data to be stored as native file storage type. Nullable data is the same length as nonnull data, and character data is always stored in character format.
\*For more information about the **decimal** and **numeric** data types, see [decimal and numeric (Transact-SQL)](../../t-sql/data-types/decimal-and-numeric-transact-sql.md).
118
-
119
-
In all of the preceding cases, to create a data file for later reloading into [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] that keeps the storage space to a minimum, use a length prefix with the default file storage type and the default field length.
[Specify Field and Row Terminators (SQL Server)](../../relational-databases/import-export/specify-field-and-row-terminators-sql-server.md)
126
-
[Specify Prefix Length in Data Files by Using bcp (SQL Server)](../../relational-databases/import-export/specify-prefix-length-in-data-files-by-using-bcp-sql-server.md)
127
-
[Specify File Storage Type by Using bcp (SQL Server)](../../relational-databases/import-export/specify-file-storage-type-by-using-bcp-sql-server.md)
128
-
[Keep Nulls or Use Default Values During Bulk Import (SQL Server)](../../relational-databases/import-export/keep-nulls-or-use-default-values-during-bulk-import-sql-server.md)
<sup>1</sup> For more information about the **decimal** and **numeric** data types, see [decimal and numeric (Transact-SQL)](../../t-sql/data-types/decimal-and-numeric-transact-sql.md).
125
+
126
+
In all of the preceding cases, to create a data file for later reloading into [!INCLUDE [ssNoVersion](../../includes/ssnoversion-md.md)] that keeps the storage space to a minimum, use a length prefix with the default file storage type and the default field length.
0 commit comments