# ColumnSchema

> Represents the metadata of a column in a Microsoft SQL Server database table.

[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) › ColumnSchema

| class | `yii\db\mssql\ColumnSchema` |
| --- | --- |
| Extends | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0.23 |
| Source | [`framework/db/mssql/ColumnSchema.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/db/mssql/ColumnSchema.php#L37) |

Represents the metadata of a column in a Microsoft SQL Server database table.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-allowNull"></span>[`$allowNull`](/docs/api/yii/db/columnschema/index.md#property-allowNull) | `bool` | Whether this column can be null. Default: `false`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-autoIncrement"></span>[`$autoIncrement`](/docs/api/yii/db/columnschema/index.md#property-autoIncrement) | `bool` | Whether this column is auto-incremental Default: `false`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-comment"></span>[`$comment`](/docs/api/yii/db/columnschema/index.md#property-comment) | `string\|null` | Comment of this column. Default: `null`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-dbType"></span>[`$dbType`](/docs/api/yii/db/columnschema/index.md#property-dbType) | `string` | The DB type of this column. Default: `''`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-defaultValue"></span>[`$defaultValue`](/docs/api/yii/db/columnschema/index.md#property-defaultValue) | `mixed` | Default value of this column Default: `null`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-enumValues"></span>[`$enumValues`](/docs/api/yii/db/columnschema/index.md#property-enumValues) | `array\|null` | Enumerable values. Default: `null`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-isComputed"></span>`$isComputed` | `bool` | Whether this column is a computed column Default: `false`. Available since 2.0.39. | `ColumnSchema` |
| <span id="property-isPrimaryKey"></span>[`$isPrimaryKey`](/docs/api/yii/db/columnschema/index.md#property-isPrimaryKey) | `bool\|null` | Whether this column is a primary key Default: `null`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-name"></span>[`$name`](/docs/api/yii/db/columnschema/index.md#property-name) | `string` | Name of this column (without quotes). Default: `''`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-outputColumnDeclaration"></span>`$outputColumnDeclaration` read-only | `string` | SQL type declaration. | `ColumnSchema` |
| <span id="property-phpType"></span>[`$phpType`](/docs/api/yii/db/columnschema/index.md#property-phpType) | `string` | The PHP type of this column. Default: `''`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-precision"></span>[`$precision`](/docs/api/yii/db/columnschema/index.md#property-precision) | `int\|null` | Precision of the column data, if it is numeric. Default: `null`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-scale"></span>[`$scale`](/docs/api/yii/db/columnschema/index.md#property-scale) | `int\|null` | Scale of the column data, if it is numeric. Default: `null`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-size"></span>[`$size`](/docs/api/yii/db/columnschema/index.md#property-size) | `int\|null` | Display size of the column. Default: `null`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-type"></span>[`$type`](/docs/api/yii/db/columnschema/index.md#property-type) | `string` | Abstract type of this column. Default: `''`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| <span id="property-unsigned"></span>[`$unsigned`](/docs/api/yii/db/columnschema/index.md#property-unsigned) | `bool` | Whether this column is unsigned. Default: `false`. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`dbTypecast()`](#method-dbTypecast) | Renders a `rowversion`/`timestamp` value as an 8-byte binary literal (`0x...`) for WHERE comparisons, and converts `varbinary` strings to `CONVERT(VARBINARY(MAX), 0x...)` expressions to avoid conversion errors. | `ColumnSchema` |
| [`defaultPhpTypecast()`](#method-defaultPhpTypecast) | Converts a MSSQL column default value to its PHP representation. | `ColumnSchema` |
| [`getOutputColumnDeclaration()`](#method-getOutputColumnDeclaration) | Returns the SQL type declaration for this column inside an OUTPUT clause temp table. | `ColumnSchema` |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`isRowVersion()`](#method-isRowVersion) | Returns whether this column is a SQL Server `rowversion` (legacy synonym `timestamp`) auto-versioning column. | `ColumnSchema` |
| <span id="method-isType"></span>[`isType()`](/docs/api/yii/db/columnschema/index.md#method-isType) | Returns whether this column uses the given abstract type. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |
| [`phpTypecast()`](#method-phpTypecast) | Converts the input value according to [`$phpType`](/docs/api/yii/db/columnschema/index.md#property-phpType) after retrieval from the database. | `ColumnSchema` |
| <span id="method-typecast"></span>[`typecast()`](/docs/api/yii/db/columnschema/index.md#method-typecast) protected | Converts the input value according to [`$phpType`](/docs/api/yii/db/columnschema/index.md#property-phpType) after retrieval from the database. | [`yii\db\ColumnSchema`](/docs/api/yii/db/columnschema/index.md) |

## Method details

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

```php
public function dbTypecast(mixed $value): mixed
```

Renders a `rowversion`/`timestamp` value as an 8-byte binary literal (`0x...`) for WHERE comparisons, and
converts `varbinary` strings to `CONVERT(VARBINARY(MAX), 0x...)` expressions to avoid conversion errors.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `mixed` |  |

**Returns**

`mixed`

**See also**

- <https://github.com/yiisoft/yii2/issues/9653>
- <https://github.com/yiisoft/yii2/issues/12599>

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

```php
public function defaultPhpTypecast(mixed $value): mixed
```

Converts a MSSQL column default value to its PHP representation.

Recognizes the literal formats stored in `sys.default_constraints.definition` and typecasts only those:
- `NULL` or any parenthesized form such as `(NULL)` to `null`.
- Quoted strings (`('value')` / `(N'value')`, any parenthesis depth) to the unquoted value with doubled quotes
  unescaped, delegated to [`phpTypecast()`](/docs/api/yii/db/mssql/columnschema/index.md#method-phpTypecast).
- Parenthesized signed, decimal, or scientific numerics (`((0))`, `((-5))`, `((1.0e+005))`) delegated to
  [`phpTypecast()`](/docs/api/yii/db/mssql/columnschema/index.md#method-phpTypecast).
- Binary literals (`(0x...)`) to their byte string via `hex2bin()`.

Everything else; function calls such as `(getdate())`, operator expressions such as `((1)+(2))`, or
`(NEXT VALUE FOR ...)` sequences; becomes an executable [`Expression`](/docs/api/yii/db/expression/index.md): the definition is SQL expression text
and MSSQL exposes no literal-versus-expression metadata.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `mixed` | Default value in MSSQL `column_default` format. |

**Returns**

`mixed`: Converted value.

**Since**

2.0.24

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

```php
public function getOutputColumnDeclaration(): string
```

Returns the SQL type declaration for this column inside an OUTPUT clause temp table.

Preserves the reflected declaration for variable-length types (`varchar`, `nvarchar`, `varbinary`), keeps the
legacy `(MAX)` fallback for bare variable-length declarations, appends the declared size for fixed-length types
(`char`, `nchar`, `binary`), and maps `timestamp` to `varbinary(8)` or `binary(8)`.

**Returns**

`string`: SQL type declaration.

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

```php
public function isRowVersion(): bool
```

Returns whether this column is a SQL Server `rowversion` (legacy synonym `timestamp`) auto-versioning column.

**Returns**

`bool`

**See also**

- <https://learn.microsoft.com/en-us/sql/t-sql/data-types/rowversion-transact-sql>

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

```php
public function phpTypecast(mixed $value): mixed
```

Converts the input value according to [`$phpType`](/docs/api/yii/db/columnschema/index.md#property-phpType) after retrieval from the database.

Decodes a `rowversion`/`timestamp` token to its integer value, and reads `varbinary` streams into strings.

If the value is null or an [`Expression`](/docs/api/yii/db/expression/index.md), it will not be converted.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `mixed` | Input value |

**Returns**

`mixed`: Converted value