# Data Types

Nikhil
Published on 2021-08-13 15:51:28

## DECIMAL and NUMERIC

Fixed precision and scale decimal numbers. DECIMAL and NUMERIC are functionally equivalent.

Syntax:

DECIMAL ( precision [ , scale] )
NUMERIC ( precision [ , scale] )

Examples:

SELECT CAST(123 AS DECIMAL(5,2)) --returns 123.00
SELECT CAST(12345.12 AS NUMERIC(10,5)) --returns 12345.12000

## FLOAT and REAL

Approximate-number data types for use with floating point numeric data.

SELECT CAST( PI() AS FLOAT) --returns 3.14159265358979
SELECT CAST( PI() AS REAL) --returns 3.141593

## Integers

Exact-number data types that use integer data.

Data type Range Storage

bigint -2^63 (-9,223,372,036,854,775,808) to 2^63-1 (9,223,372,036,854,775,807) 8 Bytes
int -2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647) 4 Bytes
smallint -2^15 (-32,768) to 2^15-1 (32,767) 2 Bytes
tinyint 0 to 255 1 Byte

## MONEY and SMALLMONEY

Data types that represent monetary or currency values.

Data type Range Storage

money -922,337,203,685,477.5808 to 922,337,203,685,477.5807 8 bytes
smallmoney -214,748.3648 to 214,748.3647 4 bytes

## BINARY and VARBINARY

Binary data types of either fixed length or variable length.

Syntax:

BINARY [ ( n_bytes ) ]
VARBINARY [ ( n_bytes | max ) ]

n_bytes can be any number from 1 to 8000 bytes. max indicates that the maximum storage space is 2^31-1.

Examples:

SELECT CAST(12345 AS BINARY(10)) -- 0x00000000000000003039
SELECT CAST(12345 AS VARBINARY(10)) -- 0x00003039

## CHAR and VARCHAR

String data types of either fixed length or variable length.

Syntax:

CHAR [ ( n_chars ) ]
VARCHAR [ ( n_chars ) ]

Examples:

SELECT CAST('ABC' AS CHAR(10)) -- 'ABC ' (padded with spaces on the right)
SELECT CAST('ABC' AS VARCHAR(10)) -- 'ABC' (no padding due to variable character)
SELECT CAST('ABCDEFGHIJKLMNOPQRSTUVWXYZ' AS CHAR(10)) -- 'ABCDEFGHIJ' (truncated to 10 characters)

## NCHAR and NVARCHAR

UNICODE string data types of either fixed length or variable length.

Syntax:

NCHAR [ ( n_chars ) ]
NVARCHAR [ ( n_chars | MAX ) ]

Use MAX for very long strings that may exceed 8000 characters.

## UNIQUEIDENTIFIER

A 16-byte GUID / UUID.

DECLARE @GUID UNIQUEIDENTIFIER = NEWID();
SELECT @GUID -- 'E28B3BD9-9174-41A9-8508-899A78A33540'
SELECT

## ATutorialHub Related Guide

#### panduranga gupta

2021-07-05 07:03:13

good website for learning and help me a lot

#### raju

2021-09-25 14:58:47

The awsome website i am looking like for a long time, good work atutorialhub team keep doing

#### Shivani

2021-09-01 15:03:56

Learning a lot from the courses present on atutorialhub. The courses are very well explained. Great experience

#### Harshitha

2021-09-10 15:05:45

It is very helpful to students and easy to learn the concepts

#### Sowmya

2021-09-14 15:06:41

Great job Tutorials are easy to understand Please make use of it

#### Zain Khan

2021-09-18 15:07:23

Great content and customized courses.

#### Rudrakshi Bhatt

2021-09-09 15:08:10

Well structured coursed and explained really well!

#### Pavana Somashekar

2021-09-11 15:09:08

Good platform for beginners and learn a lot on this website

#### Sax

2021-09-25 19:35:50

Nice website