You are on page 1of 4

10+ common questions about SQL Server data types

By Susan Sales Harkins

Version 1.0 May 15, 2008

SQL Server has a variety of data types, and as with anything, the more options you have, the more confusing a choice can be. Most misunderstandings arise from data type limitations rather than functionality. Here are the most common questions I receive about using SQL Server data types.

Use character data types to store values you dont evaluate in mathematical equations, even if the data consists of numeric characters. For instance, use a character data type to store names, addresses, ZIP codes, and phone numbers. SQL Server offers several character data types and deciding which to apply is confusing only if you dont know the differences between them. Table A gives a quick comparison of char, varchar, nchar, and nvarchar. Table A: Character data types Data Type char varchar Length Fixed Variable Storage Size Always n bytes Actual length of entry in bytes Twice n bytes Twice actual length of entry in bytes Max Characters 8,000 8,000 Unicode No; each character requires 1 byte No; each character requires 1 byte

Which character data type should I use?

nchar nvarchar

Fixed Variable

4,000 4,000

Yes; each character requires 2 bytes Yes; each character requires 2 bytes

Here are a few general rules that should help: Dont use nchar or nvarchar unless you truly need it. (Unicode provides a unique number for up to 65,536 characters. ANSI, the one most of us are most familiar with, has only 256.) Unless youre working with an international application, you probably dont need a Unicode data type. Use the smallest data type necessary, but make sure it can accommodate the largest possible value. Use a fixed-length data type when the values are mostly about the same size. Use a variable length when the values vary a lot in size.

Use integer data types to store numeric data that the application evaluates as numbers. Table B compares the four integer data types. Table B: Integer data types Data type tinyint smallint int bigint Minimum value 0 -32,768 -2,147,483,648 -9,223,372,036,854,775,808 Maximum value 255 32,767 2,147,483,674 9,223,372,036,854,775,807 Storage size 1 byte 2 bytes 4 bytes 8 bytes

Which integer type should I use?

1
Copyright 2008 CNET Networks, Inc. All rights reserved. For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html

10+ common questions about SQL Server data types Assigning the appropriate integer data type isnt as confusing as choosing a character data type. Simply use the smallest integer data type that accommodates the largest possible value.

Whats the difference between numeric and decimal?

Theres no difference between the numeric and decimal data types. Use them interchangeably or use one or the other to store integer and floating-point numbers scaled from 1 to 38 places, inclusive of both sides of a decimal. Use this data type when you need to control the accuracy of your calculations in terms of the number of decimal digits. The following table lists the exact storage size for this data type, depending on size, as listed in Table C. Table C: Precision storage requirements Total characters (precision) 1-9 10 - 19 20 - 28 29 - 38 Storage size 5 bytes 9 bytes 13 bytes 17 bytes

The only differences between float and real are their minimum and maximum values and their required storage, as compared in Table D. Use float or real to store approximate values, where precision cant be represented (e.g., Pi). Table D: Float and real data type restrictions Data type float(n) n 1 - 24 25 - 53 real n/a Minimum Value -1.79E + 308 -1.79E + 308 -3.40E + 38 Maximum value 1.79 + 308 1.79E + 308 3.40E + 38 Precision 7 digits 15 digits 7 digits Storage size 4 bytes 8 bytes 4 bytes

Whats the difference between float and real?

The real data type is the same as float(24) -- a floating data type with 24 digits to the right of the decimal point.

Whats the difference between smalldatetime and datetime?

Both smalldatetime and datetime store a combination date and time value, but the minimum and maximum values, accuracy, and storage size are different, as compared in Table E. Use datetime even when all dates fall into smalldatetimes range, if you require up-to-the-second accuracy.

2
Copyright 2008 CNET Networks, Inc. All rights reserved. For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html

10+ common questions about SQL Server data types

Table E: Smalldatetime and datetime restrictions Data type smalldatetime Minimum value January 1, 1900 Maximum value June 6, 2079 Accuracy Up to a minute Storage size 4 bytes (the first 2 bytes store the date; the second 2 bytes store the time) 8 bytes (the first 4 bytes store the date; the second 4 bytes store the time)

datetime

January 1, 1753

December 31, 9999

One threehundredth of a second

Use both smallmoney and money to store currency values. However, the minimum and maximum values for both differ, as compared in Table F. Both data types are accurate up to ten-thousandths of a monetary unit. Table F: Smallmoney and money restrictions Data type smallmoney money Minimum value -214,748.3648 -922,337,203,685,477.5808 Maximum value 214,748,3647 922,337,203.685,477.5807 Storage size 4 bytes 8 bytes

Whats the difference between smallmoney and money?

SQL Server doesnt have a Boolean data type, at least not by that name. To store True/False, Yes/No, and On/Off values, use the bit data type. It accepts only three values: 0, 1, and NULL. (NULL is supported by SQL Server 7.0 and later.)

Wheres the Boolean data type?

What happened to text, ntext, and image?

SQL Server is phasing out text, ntext, and image. Theres no way to know how long SQL Server will support the older data types. Upgrade legacy applications to varchar, nvarchar, and varbinary.

How do I assign a cursor or table data type?

You dont, at least not in the traditional manner. You dont assign these data types to a column. You can use cursor and table only as variables: The cursor data type allows you to return a cursor from a stored procedure or store a cursor as a variable. The table data type returns a table from a stored procedure or stores a table as a variable for later processing.

3
Copyright 2008 CNET Networks, Inc. All rights reserved. For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html

10+ common questions about SQL Server data types

SQL Server lets you create custom data types that are based on system data types. Create a user-defined data type when you specify the same limitations often. For instance, if many tables contain a state column, base a user-defined data type on SQL Servers nchar (see #1) with a length of 2 and name it State. Then, choose State as the columns data type, instead of specifying nchar(2). It requires about as much work, but its selfdocumenting and easy to remember. This example is simple; usually a user-defined data type is a bit more complex.

10

What is a user-defined data type?

11

Does SQL Server 2008 have any new data types?


Yes, SQL Server 2008 has several new data types:

date stores only date values with a range of 0001-01-01 through 9999-12-31. time stores only time values with a range of 00:00:00.0000000 through 23:59:59.9999999. datetime2 has a larger year and second range. datetimeoffset lets you consider times in different zones. hierarchyid constructs relationships among data elements within a table, so you can represent a position in a hierarchy. spatial identifies geographical locations and shapes -- landmarks, roads, and so on.

Susan Sales Harkins is an independent consultant and the author of several articles and books on database technologies. Her most recent book is Mastering Microsoft SQL Server 2005 Express, with Mike Gunderloy, published by Sybex. Other collaborations with Gunderloy are Automating Microsoft Access 2003 with VBA, Upgraders Guide to Microsoft Office System 2003, ICDL Exam Cram 2, and Absolute Beginner's Guide to Microsoft Access 2003, all published by Que. Currently, Susan volunteers as the Publications Director for Database Advisors. You can reach her at ssharkins@gmail.com.

Additional resources
TechRepublic's Downloads RSS Feed Sign up for the Downloads at TechRepublic newsletter Sign up for our IT Leadership Newsletter Check out all of TechRepublic's free newsletters 10 reasons to explicitly convert SQL Server data types Five SQL Server scripts for novice DBAs How do I... Reject alpha characters in a SQL Server character column?

Version history
Version: 1.0 Published: May 15, 2008

4
Copyright 2008 CNET Networks, Inc. All rights reserved. For more downloads and a free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html

You might also like