Use TYPEPROPERTY() to Return Information about a Data Type in SQL Server

In SQL Server, you can use the TYPEPROPERTY() function to return information about a data type.

You provide the name of the data type, as well as the property you want returned, and it returns information about that property for the given data type.

You can use TYPEPROPERTY() for system data types and user-defined data types.

Syntax

The syntax goes like this:

TYPEPROPERTY (type , property)

Where type is the data type, and property is the particular property that you want information about.

There are 5 possible properties: AllowsNull, OwnerId, Precision, Scale, and UsesAnsiTrim.

For more detailed information on these properties and how they work with this function, see the Microsoft documentation.

Example 1 – Basic Usage

Here’s a basic example to demonstrate how it works.

SELECT TYPEPROPERTY('int', 'Precision') AS Result;

Result:

+----------+
| Result   |
|----------|
| 10       |
+----------+

This result tells us that the int data type has a precision of 10.

Example 2 – All Properties

Here’s an example that returns all properties for the datetime2 data type.

SELECT 
  TYPEPROPERTY( 'datetime2', 'AllowsNull') AS [AllowsNull],
  TYPEPROPERTY( 'datetime2', 'OwnerId') AS [OwnerId],
  TYPEPROPERTY( 'datetime2', 'Precision') AS [Precision],
  TYPEPROPERTY( 'datetime2', 'Scale') AS [Scale],
  TYPEPROPERTY( 'datetime2', 'UsesAnsiTrim') AS [UsesAnsiTrim];

Result:

+--------------+-----------+-------------+---------+----------------+
| AllowsNull   | OwnerId   | Precision   | Scale   | UsesAnsiTrim   |
|--------------+-----------+-------------+---------+----------------|
| 1            | 4         | 27          | 7       | NULL           |
+--------------+-----------+-------------+---------+----------------+

The reason UsesAnsiTrim is NULL is because that property is only applicable for binary and string data types.

To demonstrate, here it is again, except using a string data type:

SELECT 
  TYPEPROPERTY( 'varchar', 'AllowsNull') AS [AllowsNull],
  TYPEPROPERTY( 'varchar', 'OwnerId') AS [OwnerId],
  TYPEPROPERTY( 'varchar', 'Precision') AS [Precision],
  TYPEPROPERTY( 'varchar', 'Scale') AS [Scale],
  TYPEPROPERTY( 'varchar', 'UsesAnsiTrim') AS [UsesAnsiTrim];

Result:

+--------------+-----------+-------------+---------+----------------+
| AllowsNull   | OwnerId   | Precision   | Scale   | UsesAnsiTrim   |
|--------------+-----------+-------------+---------+----------------|
| 1            | 4         | 8000        | NULL    | 1              |
+--------------+-----------+-------------+---------+----------------+

This time Scale is NULL. That’s because this property is only applicable for numeric and certain date types.

Example 3 – Specifying Scale or Precision

Some data types allow you to specify a scale or precision, but TYPEPROPERTY() won’t accept that:

SELECT 
  TYPEPROPERTY( 'datetime2(3)', 'Scale') AS [Datetime2 Scale],
  TYPEPROPERTY( 'datetime2(3)', 'Precision') AS [Datetime2 Precision],
  TYPEPROPERTY( 'varchar(255)', 'Precision') AS [Varchar Precision];

Result:

+-------------------+-----------------------+---------------------+
| Datetime2 Scale   | Datetime2 Precision   | Varchar Precision   |
|-------------------+-----------------------+---------------------|
| NULL              | NULL                  | NULL                |
+-------------------+-----------------------+---------------------+

Example 4 – User-Defined Data Types

The TYPEPROPERTY() function also works on user-defined data types.

Here’s an example.

SELECT 
  TYPEPROPERTY( 'clientcode', 'AllowsNull') AS [AllowsNull],
  TYPEPROPERTY( 'clientcode', 'OwnerId') AS [OwnerId],
  TYPEPROPERTY( 'clientcode', 'Precision') AS [Precision],
  TYPEPROPERTY( 'clientcode', 'Scale') AS [Scale],
  TYPEPROPERTY( 'clientcode', 'UsesAnsiTrim') AS [UsesAnsiTrim];

Result:

+--------------+-----------+-------------+---------+----------------+
| AllowsNull   | OwnerId   | Precision   | Scale   | UsesAnsiTrim   |
|--------------+-----------+-------------+---------+----------------|
| 0            | 1         | 8           | NULL    | 1              |
+--------------+-----------+-------------+---------+----------------+

Example 5 – Including the Data Type Schema Prefix

You can also prefix the data type with the schema name.

SELECT 
  TYPEPROPERTY( 'sys.int', 'AllowsNull') AS [AllowsNull],
  TYPEPROPERTY( 'sys.int', 'OwnerId') AS [OwnerId],
  TYPEPROPERTY( 'sys.int', 'Precision') AS [Precision],
  TYPEPROPERTY( 'sys.int', 'Scale') AS [Scale],
  TYPEPROPERTY( 'sys.int', 'UsesAnsiTrim') AS [UsesAnsiTrim];

Result:

+--------------+-----------+-------------+---------+----------------+
| AllowsNull   | OwnerId   | Precision   | Scale   | UsesAnsiTrim   |
|--------------+-----------+-------------+---------+----------------|
| 1            | 4         | 10          | 0       | NULL           |
+--------------+-----------+-------------+---------+----------------+

Example 6 – Return Info for All Data Types in the Database

Here’s an example that returns property information for all data types in the current database.

To do this, I query the sys.types system view. I also use the SCHEMA_NAME() function to get the schema’s name based on its ID.

SELECT 
  SCHEMA_NAME(schema_id) AS [Schema],
  name AS [Data Type], 
  TYPEPROPERTY(SCHEMA_NAME(schema_id) + '.' + name, 'AllowsNull') AS [AllowsNull],
  TYPEPROPERTY(SCHEMA_NAME(schema_id) + '.' + name, 'OwnerId') AS [OwnerId],
  TYPEPROPERTY(SCHEMA_NAME(schema_id) + '.' + name, 'Precision') AS [Precision],
  TYPEPROPERTY(SCHEMA_NAME(schema_id) + '.' + name, 'Scale') AS [Scale],
  TYPEPROPERTY(SCHEMA_NAME(schema_id) + '.' + name, 'UsesAnsiTrim') AS [UsesAnsiTrim]
FROM sys.types
ORDER BY name;

Result:

+----------+------------------+--------------+-----------+-------------+---------+----------------+
| Schema   | Data Type        | AllowsNull   | OwnerId   | Precision   | Scale   | UsesAnsiTrim   |
|----------+------------------+--------------+-----------+-------------+---------+----------------|
| sys      | bigint           | 1            | 4         | 19          | 0       | NULL           |
| sys      | binary           | 1            | 4         | 8000        | NULL    | 1              |
| sys      | bit              | 1            | 4         | 1           | NULL    | NULL           |
| sys      | char             | 1            | 4         | 8000        | NULL    | 1              |
| dbo      | clientcode       | 0            | 1         | 8           | NULL    | 1              |
| sys      | date             | 1            | 4         | 10          | 0       | NULL           |
| sys      | datetime         | 1            | 4         | 23          | 3       | NULL           |
| sys      | datetime2        | 1            | 4         | 27          | 7       | NULL           |
| sys      | datetimeoffset   | 1            | 4         | 34          | 7       | NULL           |
| sys      | decimal          | 1            | 4         | 38          | 38      | NULL           |
| sys      | float            | 1            | 4         | 53          | NULL    | NULL           |
| sys      | geography        | 1            | 4         | -1          | NULL    | NULL           |
| sys      | geometry         | 1            | 4         | -1          | NULL    | NULL           |
| sys      | hierarchyid      | 1            | 4         | 892         | NULL    | NULL           |
| sys      | image            | 1            | 4         | 2147483647  | NULL    | NULL           |
| sys      | int              | 1            | 4         | 10          | 0       | NULL           |
| sys      | money            | 1            | 4         | 19          | 4       | NULL           |
| sys      | nchar            | 1            | 4         | 4000        | NULL    | NULL           |
| sys      | ntext            | 1            | 4         | 1073741823  | NULL    | NULL           |
| sys      | numeric          | 1            | 4         | 38          | 38      | NULL           |
| sys      | nvarchar         | 1            | 4         | 4000        | NULL    | NULL           |
| sys      | real             | 1            | 4         | 24          | NULL    | NULL           |
| sys      | smalldatetime    | 1            | 4         | 16          | 0       | NULL           |
| sys      | smallint         | 1            | 4         | 5           | 0       | NULL           |
| sys      | smallmoney       | 1            | 4         | 10          | 4       | NULL           |
| sys      | sql_variant      | 1            | 4         | 0           | NULL    | 1              |
| sys      | sysname          | 0            | 4         | 128         | NULL    | NULL           |
| sys      | text             | 1            | 4         | 2147483647  | NULL    | NULL           |
| sys      | time             | 1            | 4         | 16          | 7       | NULL           |
| sys      | timestamp        | 0            | 4         | 8           | NULL    | NULL           |
| sys      | tinyint          | 1            | 4         | 3           | 0       | NULL           |
| sys      | uniqueidentifier | 1            | 4         | 16          | NULL    | NULL           |
| sys      | varbinary        | 1            | 4         | 8000        | NULL    | 1              |
| sys      | varchar          | 1            | 4         | 8000        | NULL    | 1              |
| sys      | xml              | 1            | 4         | -1          | NULL    | NULL           |
+----------+------------------+--------------+-----------+-------------+---------+----------------+

Although bear in mind that sys.types already contains the precision and scale, so you could also do this:

SELECT 
  SCHEMA_NAME(schema_id) AS [Schema],
  name AS [Data Type], 
  TYPEPROPERTY(SCHEMA_NAME(schema_id) + '.' + name, 'AllowsNull') AS [AllowsNull],
  TYPEPROPERTY(SCHEMA_NAME(schema_id) + '.' + name, 'OwnerId') AS [OwnerId],
  Precision,
  Scale,
  TYPEPROPERTY(SCHEMA_NAME(schema_id) + '.' + name, 'UsesAnsiTrim') AS [UsesAnsiTrim]
FROM sys.types
ORDER BY name;

Result:

+----------+------------------+--------------+-----------+-------------+---------+----------------+
| Schema   | Data Type        | AllowsNull   | OwnerId   | Precision   | Scale   | UsesAnsiTrim   |
|----------+------------------+--------------+-----------+-------------+---------+----------------|
| sys      | bigint           | 1            | 4         | 19          | 0       | NULL           |
| sys      | binary           | 1            | 4         | 0           | 0       | 1              |
| sys      | bit              | 1            | 4         | 1           | 0       | NULL           |
| sys      | char             | 1            | 4         | 0           | 0       | 1              |
| dbo      | clientcode       | 0            | 1         | 0           | 0       | 1              |
| sys      | date             | 1            | 4         | 10          | 0       | NULL           |
| sys      | datetime         | 1            | 4         | 23          | 3       | NULL           |
| sys      | datetime2        | 1            | 4         | 27          | 7       | NULL           |
| sys      | datetimeoffset   | 1            | 4         | 34          | 7       | NULL           |
| sys      | decimal          | 1            | 4         | 38          | 38      | NULL           |
| sys      | float            | 1            | 4         | 53          | 0       | NULL           |
| sys      | geography        | 1            | 4         | 0           | 0       | NULL           |
| sys      | geometry         | 1            | 4         | 0           | 0       | NULL           |
| sys      | hierarchyid      | 1            | 4         | 0           | 0       | NULL           |
| sys      | image            | 1            | 4         | 0           | 0       | NULL           |
| sys      | int              | 1            | 4         | 10          | 0       | NULL           |
| sys      | money            | 1            | 4         | 19          | 4       | NULL           |
| sys      | nchar            | 1            | 4         | 0           | 0       | NULL           |
| sys      | ntext            | 1            | 4         | 0           | 0       | NULL           |
| sys      | numeric          | 1            | 4         | 38          | 38      | NULL           |
| sys      | nvarchar         | 1            | 4         | 0           | 0       | NULL           |
| sys      | real             | 1            | 4         | 24          | 0       | NULL           |
| sys      | smalldatetime    | 1            | 4         | 16          | 0       | NULL           |
| sys      | smallint         | 1            | 4         | 5           | 0       | NULL           |
| sys      | smallmoney       | 1            | 4         | 10          | 4       | NULL           |
| sys      | sql_variant      | 1            | 4         | 0           | 0       | 1              |
| sys      | sysname          | 0            | 4         | 0           | 0       | NULL           |
| sys      | text             | 1            | 4         | 0           | 0       | NULL           |
| sys      | time             | 1            | 4         | 16          | 7       | NULL           |
| sys      | timestamp        | 0            | 4         | 0           | 0       | NULL           |
| sys      | tinyint          | 1            | 4         | 3           | 0       | NULL           |
| sys      | uniqueidentifier | 1            | 4         | 0           | 0       | NULL           |
| sys      | varbinary        | 1            | 4         | 0           | 0       | 1              |
| sys      | varchar          | 1            | 4         | 0           | 0       | 1              |
| sys      | xml              | 1            | 4         | 0           | 0       | NULL           |
+----------+------------------+--------------+-----------+-------------+---------+----------------+