# Quoter

> Provides connection-free helpers for quoting MSSQL values and identifiers when building SQL.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [db](/docs/api/yii/db/index.md) › [mssql](/docs/api/yii/db/mssql/index.md) › Quoter

| class | `yii\db\mssql\Quoter` |
| --- | --- |
| Since | 22.0 |
| Source | [`framework/db/mssql/Quoter.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/db/mssql/Quoter.php#L28) |

Provides connection-free helpers for quoting MSSQL values and identifiers when building SQL.

Unlike [`Schema::quoteValue()`](/docs/api/yii/db/schema/index.md#method-quoteValue), the helpers never open a database connection, so they are safe to call
while [`QueryBuilder`](/docs/api/yii/db/mssql/querybuilder/index.md) assembles dynamic SQL ahead of execution.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`escapeLiteralValue()`](#method-escapeLiteralValue) static | Escapes single quotes in a value for embedding as a Transact-SQL string literal. | `Quoter` |
| [`extractSimpleIdentifier()`](#method-extractSimpleIdentifier) static | Returns the trailing single-part identifier of a bracket-quoted qualified name, keeping its brackets. | `Quoter` |
| [`isIdentifierBracketQuoted()`](#method-isIdentifierBracketQuoted) static | Returns whether an identifier is already wrapped in MSSQL square brackets (`[identifier]`). | `Quoter` |

## Method details

### escapeLiteralValue() <span id="method-escapeLiteralValue"></span>

```php
public static function escapeLiteralValue(string $value): string
```

Escapes single quotes in a value for embedding as a Transact-SQL string literal.

Doubles every single quote (`'` becomes `''`) without adding the surrounding quote characters, so the result can
be placed inside `'...'` or `N'...'`.

Usage example:
```php
$literal = \yii\db\mssql\Quoter::escapeLiteralValue("O'Brien"); // O''Brien
$sql = "DECLARE @name SYSNAME = N'{$literal}'";
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `string` | Value to escape. |

**Returns**

`string`: Value with single quotes doubled, ready to embed inside a quoted literal.

### extractSimpleIdentifier() <span id="method-extractSimpleIdentifier"></span>

```php
public static function extractSimpleIdentifier(string $name): string
```

Returns the trailing single-part identifier of a bracket-quoted qualified name, keeping its brackets.

Splits on the last `].[` boundary; a name without that boundary is returned unchanged.

Usage example:
```php
\yii\db\mssql\Quoter::extractSimpleIdentifier('[schema].[table]'); // [table]
\yii\db\mssql\Quoter::extractSimpleIdentifier('[table]');          // [table]
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Bracket-quoted qualified identifier. |

**Returns**

`string`: Trailing single-part identifier with brackets preserved.

### isIdentifierBracketQuoted() <span id="method-isIdentifierBracketQuoted"></span>

```php
public static function isIdentifierBracketQuoted(string $identifier): bool
```

Returns whether an identifier is already wrapped in MSSQL square brackets (`[identifier]`).

Canonical predicate for an already bracket-quoted identifier; the value starts with `[` and ends with `]`.

Usage example:
```php
\yii\db\mssql\Quoter::isIdentifierBracketQuoted('[user]'); // true
\yii\db\mssql\Quoter::isIdentifierBracketQuoted('user');   // false
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$identifier` | `string` | Identifier to inspect. |

**Returns**

`bool`: Whether the identifier is already bracket-quoted.