Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit bd0398b

Browse files
committed
Refresh string and binary data type articles
1 parent e0d7e6b commit bd0398b

6 files changed

Lines changed: 207 additions & 178 deletions

docs/t-sql/data-types/binary-and-varbinary-transact-sql.md

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ title: "binary and varbinary (Transact-SQL)"
33
description: "binary and varbinary (Transact-SQL)"
44
author: MikeRayMSFT
55
ms.author: mikeray
6-
ms.date: "08/16/2017"
6+
ms.reviewer: randolphwest
7+
ms.date: 09/22/2022
78
ms.prod: sql
89
ms.technology: t-sql
910
ms.topic: "reference"
@@ -25,74 +26,76 @@ monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-s
2526
[!INCLUDE [sql-asdb-asdbmi-asa-pdw](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw.md)]
2627

2728
Binary data types of either fixed length or variable length.
28-
29+
2930
## Arguments
3031

31-
**binary** [ ( _n_ ) ]
32-
Fixed-length binary data with a length of _n_ bytes, where _n_ is a value from 1 through 8,000. The storage size is _n_ bytes.
33-
34-
**varbinary** [ ( _n_ | **max**) ]
35-
Variable-length binary data. _n_ can be a value from 1 through 8,000. **max** indicates that the maximum storage size is 2^31-1 bytes. The storage size is the actual length of the data entered + 2 bytes. The data that is entered can be 0 bytes in length. The ANSI SQL synonym for **varbinary** is **binary varying**.
36-
37-
## Remarks
38-
The default length is 1 when _n_ isn't specified in a data definition or variable declaration statement. When _n_ isn't specified with the CAST function, the default length is 30.
32+
#### binary [ ( *n* ) ]
33+
34+
Fixed-length binary data with a length of *n* bytes, where *n* is a value from 1 through 8,000. The storage size is *n* bytes.
35+
36+
#### varbinary [ ( *n* | max ) ]
37+
38+
Variable-length binary data. *n* can be a value from 1 through 8,000. **max** indicates that the maximum storage size is 2^31-1 bytes. The storage size is the actual length of the data entered + 2 bytes. The data that is entered can be 0 bytes in length. The ANSI SQL synonym for **varbinary** is **binary varying**.
39+
40+
## Remarks
41+
42+
The default length is 1 when *n* isn't specified in a data definition or variable declaration statement. When *n* isn't specified with the `CAST` function, the default length is 30.
3943

4044
| Data type | Use when ... |
4145
| --- | --- |
4246
| **binary** | the sizes of the column data entries are consistent.|
4347
| **varbinary** | the sizes of the column data entries vary considerably.|
4448
| **varbinary(max)** | the column data entries exceed 8,000 bytes.|
4549

50+
## Convert binary and varbinary data
4651

47-
## Converting binary and varbinary data
4852
When converting data from a string data type to a **binary** or **varbinary** data type of unequal length, [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] pads or truncates the data on the right. These string data types are:
4953

50-
* **char**
51-
* **varchar**
52-
* **nchar**
53-
* **nvarchar**
54-
* **binary**
55-
* **varbinary**
56-
* **text**
57-
* **ntext**
58-
* **image**
54+
- **char**
55+
- **varchar**
56+
- **nchar**
57+
- **nvarchar**
58+
- **binary**
59+
- **varbinary**
60+
- **text**
61+
- **ntext**
62+
- **image**
5963

6064
When other data types are converted to **binary** or **varbinary**, the data is padded or truncated on the left. Padding is achieved by using hexadecimal zeros.
61-
65+
6266
Converting data to the **binary** and **varbinary** data types is useful if **binary** data is the easiest way to move around data. At some point, you might convert a value type to a binary value of large enough size and then convert it back. This conversion always results in the same value if both conversions are taking place on the same version of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]. The binary representation of a value might change from version to version of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)].
63-
67+
6468
You can convert **int**, **smallint**, and **tinyint** to **binary** or **varbinary**. If you convert the **binary** value back to an integer value, this value will be different from the original integer value if truncation has occurred. For example, the following SELECT statement shows that the integer value `123456` is stored as a binary `0x0001e240`:
65-
69+
6670
```sql
67-
SELECT CAST( 123456 AS BINARY(4) );
68-
```
69-
71+
SELECT CAST( 123456 AS BINARY(4) );
72+
```
73+
7074
However, the following `SELECT` statement shows that if the **binary** target is too small to hold the entire value, the leading digits are silently truncated so that the same number is stored as `0xe240`:
71-
75+
7276
```sql
73-
SELECT CAST( 123456 AS BINARY(2) );
74-
```
75-
77+
SELECT CAST( 123456 AS BINARY(2) );
78+
```
79+
7680
The following batch shows that this silent truncation can affect arithmetic operations without raising an error:
77-
81+
7882
```sql
79-
DECLARE @BinaryVariable2 BINARY(2);
80-
81-
SET @BinaryVariable2 = 123456;
82-
SET @BinaryVariable2 = @BinaryVariable2 + 1;
83+
DECLARE @BinaryVariable2 BINARY(2);
8384

84-
SELECT CAST( @BinaryVariable2 AS INT);
85-
GO
86-
```
85+
SET @BinaryVariable2 = 123456;
86+
SET @BinaryVariable2 = @BinaryVariable2 + 1;
8787

88+
SELECT CAST( @BinaryVariable2 AS INT);
89+
GO
90+
```
91+
8892
The final result is `57921`, not `123457`.
89-
93+
9094
> [!NOTE]
91-
> Conversions between any data type and the **binary** data types are not guaranteed to be the same between versions of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)].
92-
95+
> Conversions between any data type and the **binary** data types are not guaranteed to be the same between versions of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)].
96+
9397
## See also
94-
[CAST and CONVERT (Transact-SQL)](../../t-sql/functions/cast-and-convert-transact-sql.md)
95-
[Data Type Conversion (Database Engine)](../../t-sql/data-types/data-type-conversion-database-engine.md)
96-
[Data Types (Transact-SQL)](../../t-sql/data-types/data-types-transact-sql.md)
97-
98-
98+
99+
- [CAST and CONVERT (Transact-SQL)](../../t-sql/functions/cast-and-convert-transact-sql.md)
100+
- [Data Type Conversion (Database Engine)](../../t-sql/data-types/data-type-conversion-database-engine.md)
101+
- [Data Types (Transact-SQL)](../../t-sql/data-types/data-types-transact-sql.md)

0 commit comments

Comments
 (0)