Tutorial by Examples

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
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
Exact-number data types that use integer data. Data typeRangeStoragebigint-2^63 (-9,223,372,036,854,775,808) to 2^63-1 (9,223,372,036,854,775,807)8 Bytesint-2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647)4 Bytessmallint-2^15 (-32,768) to 2^15-1 (32,767)2 Bytestinyint0 to 2551 Byte
Data types that represent monetary or currency values. Data typeRangeStoragemoney-922,337,203,685,477.5808 to 922,337,203,685,477.58078 bytessmallmoney-214,748.3648 to 214,748.36474 bytes
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)) -- 0x0000000000000000...
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) SELE...
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.
A 16-byte GUID / UUID. DECLARE @GUID UNIQUEIDENTIFIER = NEWID(); SELECT @GUID -- 'E28B3BD9-9174-41A9-8508-899A78A33540' DECLARE @bad_GUID_string VARCHAR(100) = 'E28B3BD9-9174-41A9-8508-899A78A33540_foobarbaz' SELECT @bad_GUID_string, -- 'E28B3BD9-9174-41A9-8508-899A78A33540_foobarbaz' ...

Page 1 of 1