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
Binary data types of either fixed length or variable length.
28
-
29
+
29
30
## Arguments
30
31
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.
39
43
40
44
| Data type | Use when ... |
41
45
| --- | --- |
42
46
|**binary**| the sizes of the column data entries are consistent.|
43
47
|**varbinary**| the sizes of the column data entries vary considerably.|
44
48
|**varbinary(max)**| the column data entries exceed 8,000 bytes.|
45
49
50
+
## Convert binary and varbinary data
46
51
47
-
## Converting binary and varbinary data
48
52
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:
49
53
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**
59
63
60
64
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
+
62
66
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
+
64
68
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
+
66
70
```sql
67
-
SELECT CAST( 123456AS BINARY(4) );
68
-
```
69
-
71
+
SELECT CAST( 123456AS BINARY(4) );
72
+
```
73
+
70
74
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
+
72
76
```sql
73
-
SELECT CAST( 123456AS BINARY(2) );
74
-
```
75
-
77
+
SELECT CAST( 123456AS BINARY(2) );
78
+
```
79
+
76
80
The following batch shows that this silent truncation can affect arithmetic operations without raising an error:
77
-
81
+
78
82
```sql
79
-
DECLARE @BinaryVariable2 BINARY(2);
80
-
81
-
SET @BinaryVariable2 =123456;
82
-
SET @BinaryVariable2 = @BinaryVariable2 +1;
83
+
DECLARE @BinaryVariable2 BINARY(2);
83
84
84
-
SELECT CAST( @BinaryVariable2 ASINT);
85
-
GO
86
-
```
85
+
SET @BinaryVariable2 =123456;
86
+
SET @BinaryVariable2 = @BinaryVariable2 +1;
87
87
88
+
SELECT CAST( @BinaryVariable2 ASINT);
89
+
GO
90
+
```
91
+
88
92
The final result is `57921`, not `123457`.
89
-
93
+
90
94
> [!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
+
93
97
## 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)
0 commit comments