title | description | author | ms.author | ms.reviewer | ms.date | ms.service | ms.subservice | ms.topic | ms.custom | f1_keywords | helpviewer_keywords | dev_langs | monikerRange | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
||= (Compound assignment) (Transact-SQL) |
Use ||= to concatenate an expression with the value of a character or binary string variable, and assign the resulting expression to the variable. |
abhimantiwari |
abhtiwar |
randolphwest, wiassaf, umajay |
06/04/2024 |
sql |
t-sql |
reference |
|
|
|
|
=azuresqldb-current || =fabric |
[!INCLUDE asdb-fabricsqldb]
The ||=
concatenation with compound assignment operator can be used to concatenate an expression with the value of a character or binary string variable, and then assign the resulting expression to the variable.
The ||=
operator supports the same behavior as the += operator for character and binary strings.
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions
variable ||= expression
A T-SQL variable of character type: char, varchar, nchar, nvarchar, varchar(max), or nvarchar(max), or of binary type: binary or varbinary or varbinary(max).
A character or binary expression. If the expression isn't of the character type, then the type of the expression must be able to be implicitly converted to a character string.
Assigns the result of the concatenation operator for character strings to the variable.
- If the variable or expression is a SQL
NULL
value, then the result of the concatenated expression isNULL
. - If the variable is of a large object (LOB) data type (varchar(max) or nvarchar(max)), then the resulting expression is of varchar(max) or nvarchar(max).
- If the variable is of a LOB type varbinary(max), then the resulting expression is of varbinary(max).
- If the variable isn't a LOB type, then the result is truncated to the maximum length of declared type of the variable.
If the result of the concatenation of strings exceeds the limit of 8,000 bytes, the result is truncated. However, if at least one of the strings concatenated is a large value type, truncation doesn't occur.
An explicit conversion to character data must be used when concatenating binary strings and any characters between the binary strings.
The ||=
(string concatenation) operator behaves differently when it works with an empty, zero-length string than when it works with NULL
, or unknown values. A zero-length character string can be specified as two single quotation marks without any characters inside the quotation marks. A zero-length binary string can be specified as 0x
without any byte values specified in the hexadecimal constant. Concatenating a zero-length string always concatenates the two specified strings.
As with arithmetic operations that are performed on NULL
values, when a NULL
value is added to a known value, the result is typically a NULL
value. A string concatenation operation performed with a NULL
value should also produce a NULL
result.
The ||=
operator doesn't honor the SET CONCAT_NULL_YIELDS_NULL
option, and always behaves as if the ANSI SQL behavior is enabled, yielding NULL
if any of the inputs is NULL
. This is the primary difference in behavior between the +=
and ||=
concatenation operators. For more information, see SET CONCAT_NULL_YIELDS_NULL.
DECLARE @v1 varchar(10) = 'a'
SET @v1 ||= 'b';
SELECT @v1
[!INCLUDE ssResult]
ab
DECLARE @v2 varbinary(10) = 0x1a;
SET @v2 ||= 0x2b;
select @v2;
[!INCLUDE ssResult]
0x1A2B
- || (String concatenation) (Transact-SQL)
- + (String concatenation) (Transact-SQL)
- += (String Concatenation Assignment) (Transact-SQL)
- ALTER DATABASE (Transact-SQL)
- CAST and CONVERT (Transact-SQL)
- Data type conversion (Database Engine)
- Data types (Transact-SQL)
- Expressions (Transact-SQL)
- Built-in Functions (Transact-SQL)
- Operators (Transact-SQL)
- SELECT (Transact-SQL)
- SET Statements (Transact-SQL)