Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Data types in SQL Server

From: SQL Server 2008 Essential Training

Video: Data types in SQL Server

When you're defining your columns in SQL Server Management Studio, you will find that the data type for each column presents you with a rather large set of choices of things that you could possibly use here and it can be a little intimidating if you are new to SQL Server or you're not quite sure exactly which one you should be picking. So let's explore what some of those options are, at least what the most common ones that you want to know about. The data types are split really in two categories. Is it character or text data? Is it a number? Is it a date? What actually is this? And then we can get a little bit more specific about each of those pieces.

Data types in SQL Server

When you're defining your columns in SQL Server Management Studio, you will find that the data type for each column presents you with a rather large set of choices of things that you could possibly use here and it can be a little intimidating if you are new to SQL Server or you're not quite sure exactly which one you should be picking. So let's explore what some of those options are, at least what the most common ones that you want to know about. The data types are split really in two categories. Is it character or text data? Is it a number? Is it a date? What actually is this? And then we can get a little bit more specific about each of those pieces.

Let's start off with character or text data. Well, even when you are looking at this, you might be a little annoyed by the fact that you have six character data types: text, ntext, char, nchar, varchar, and nvarchar. And you might think, what, I am supposed to make six decisions for every single bit of text that I have? Well, actually it's a little simpler than that because straight off the bat we can lose two of these. Even though text and ntext will show up in that drop-down data type box, you should not use them.

These are old school and they're officially deprecated in this version of SQL Server, meaning they'll actually be removed from a future version. They are only there for backwards compatibility. So we're left with four and we can choose between these four simply by asking two questions. One, is the data we are going to store Unicode or non-Unicode? And two, is it a fixed length or a variable length? If we need Unicode data, that's if you support or ever expect that you might possibly support international language character sets, you need to pick Unicode.

And those are the two character data types with the letter n at the front of them, nchar and nvarchar. If you know that your text is only ever going to be plain old Western European character set, pick non- Unicode which is char and varchar without the n at the front. The impact is that if you pick Unicode, because Unicode can hold a much bigger character set, many, many different international languages, that takes up 2 bytes per character in SQL Server. If it's not Unicode, it's a char or varchar, it's 1 byte per character.

Now officially, SQL Server 2008 R2 has something called Unicode compressions. So if you are really worried about the space, you can look into that. Now, I'm just a fan of using Unicode by default all the time. So I'll pretty much always pick the one with the n in front. But the next question that I have to ask is, is this fixed length or is it variable length? And that's simply allows you to choose. If it's fixed length, it's nchar or char. If it's variable length, it's the one with the var inside it. And of course, that's going to be up to you.

What is it that you're storing? If it's product ID, for example, you might know that it's always going to be 10 characters long. If it's the contents of a blog entry, well, that's going to be a bit more variable. So you just answer these two questions, Unicode or non-Unicode, fixed length or variable length, and that will take you to the character data type that you need to pick. Once you've chosen that, you need to ask a couple of other questions. See if it's a fixed length character data like nchar or char, well, you need to then predict the amount of space that's going to take.

If it's nchar(10), meaning it's a length of 10, that will take up to 20 bytes and even if you only fill 3 characters of it, it will pad it with spaces. If it's just char of 10, it will be 10 bytes. SQL Server likes to know the size of it so it can make its own optimization about how much space this is going to take up in the system. But even if you're picking variable length character data, you need to know the impact this is going to have. If you specify an nvarchar with a length of 10 characters, well, the length of it is flexible.

It depends. The maximum of this will be 20 bytes but if you're only storing one or two characters, it should only take up the space for one or two characters. Well, one of the questions might be how long can this get? Well, if you're working with Unicode, the maximum length you can have of either an nchar and nvarchar is 4000 and that's 4000 characters. Again, because it will take, by default, 2 bytes per character. That's up to 8000 bytes here. Now because 8000 is allowed, if you are just working with the non-Unicode version, your maximum is 8000.

8000 for a varchar or a char is the maximum specified length. Well, you might ask what happens if it's bigger than that? Or if it ever could be more than 8000 bytes out of 4000 Unicode characters or 8000 regular ASCII characters? Then you have one other choice. You just use the word max or varcharmax. That allows you up to 2 GB per row per column. So you can store some pretty large stuff in here.

You just can't specify a length any larger than 4000 for Unicode and 8000 for non-Unicode. Once you get beyond that, it's max. Next up, we have numeric data types. Let's start with the exact numbers. We have the classic int or integers, storing whole numbers from roughly -2 billion to +2 billion. Nothing after the decimal point; they are always whole numbers. If you don't need that much space, you can have what's called a smallint, which is roughly -32 thousand to +32 thousand.

You've got a tinyint, which is 0 to 255. If you know that you have a very small range. But what if you want more than 2 billion? Well, you do have bigint as well, which be +/-2^63 so that should hold pretty much whatever you can come up with. We also have money and smallmoney. These are exact numbers even though they have got numbers after the decimal point. It's a fixed length. Both the money and the smallmoney data types have four digits after the decimal point.

Money can hold up to, as you can see here, that's a fairly large amount, whereas small money is +/-214,748 and then four digits after the decimal point. Formerly with your exact numbers, you also have decimal which is a fixed precision and scale. I will explain what that means in a second. Now you'll also see something called numeric. Decimal and numeric are exactly the same. Again, the two terms are here for kind of backwards compatibility. I pick decimal all the time.

It doesn't really matter. Just pick one and stick with it. What precision and scale means is as you define two numbers. The p represents the precision, the total amount of digits being stored in this column. And ,s for the scale is the amount of digits after the decimal point. So for example, creating decimal with 9,2 means there is nine digits in total and two after the decimal point. So the maximum value would be seven 9s before the point and two 9s afterwards.

So that's decimal and numeric. You do have approximate or floating point numbers. You have got float where you say exactly what the floating point should be and then you have something called real. I would suggest that if you're getting into the floating point numbers and you need to be aware of those, we will look them up on Books Online for more specific information. I will show that in just a second. You do have several date and time data types. We have one that just holds a date. We have one that just holds a time.

There is one that's called datetime. That's been around for quite a while. It holds not only the date but the time as well. Although SQL Server has something called datetime2, and that is the one that's recommended. Even if it looks kind of an ugly data type, that's the one you should use if you need to store both dates and times because it to be more accurate, down to about 100 nanoseconds, and it doesn't take as much space as datetime does. If you're working, however, with international time zones and you need to store your time offset from Universal Time Code, there is also the datetimeoffset, which takes that into account.

So you could look at that one and if you need on the other side a bit less precision, you can also look at smalldatetime. But I'd say the main ones here should be reasonably obvious, which is you just need a date, use date. You just need a time, use time. You need both, use datetime2. As you start to work with your data types, you will realize that they are not trying to give you several possible options for the same need. They really all do have pretty specific needs to them. There are several other data types. We have binary and varbinary.

As you might guess from looking the char and varchar, this really means a fixed length and a variable length binary field for storing things like file attachments. That can grow as big as 2 GB, which you will find is the fairly magic number for large column sizes in SQL Server. There is bit which is basically a field that can be true or false. You have got XML. You can store raw XML data, even though it is text, and you can think surely I could just put it in a text field. You could, but the XML column has the ability for SQL Server to understand the XML and even parse the XML if needed.

There is a unique identifier, which would create a 16 digit unique ID, what's often referred to as a GUID, a Globally Unique Identifier, and you can even define your own data types, though we are not going to do this in this course. There is a couple of other rare ones that rounded up. If you start looking through the drop-down box, you will see things like geometry and geography for ellipses and paths and even for latitude and longitude. And there is a few other deprecated ones like timestamp and image not to be used.

Here is the real deal. Until this all becomes second nature, what you want to be doing is making sure that you're using SQL Server Books Online and use that as your little quick reference for these. Let me show you the easiest way to start working with that. If I open up my SQL Server Books Online, what I'm going to do is I want to find my data type definitions. Well, I can just type in the word in my index here. So I'm looking in my index, the second tab, and I am saying data types. Now I select data types and I have all sorts of things.

It says data types here and blanks and currency and all sorts of stuff but unfortunately I am picking the wrong ones, because SQL Server Books Online is showing me a bit too much information because I got data types in PowerPivot for Excel and data types in CLR integration and data types ODBC. What I'm going to do is filter this down a little bit. Tather than have the unfiltered showing me every possibility, I'm going to say to say give me the SQL Server 2008 Database Engine and let me try that data types again.

Well we have a few still here. Data types CLR, data types ODBC, data types OLE DB. Data types SQL Server is what I'm looking for and if you are thinking, well, how do I know for sure? Well, unless, you know that you're going for OLE DB or SMO or SQL Server application, this one is a pretty good bet. And if I highlight that, I can see that it starts talking about the related data types. So I will come down in this page. Let me delete that a little bit. We've got the data type categories, we have got the bigints, and bits, and decimals, and ints.

I could jump into any of these pieces such as say smallmoney and it will tell me exactly what that range is for smallmoney. So this looks like a pretty useful page to have for my data types as I am getting used to them. And in fact what I'm going to do is go up here onto my menu bar and just click Add to Help Favorites, hit that button, and what that means is that from now on if I want to just go ahead and remind myself of the data types, I jump to my Help Favorites tab in Books Online.

I hit that and it will allow me to drop through all the different data types. The other ones, the binaries, the Unicode, character, date and time, all of it.

Show transcript

This video is part of

Image for SQL Server 2008 Essential Training
SQL Server 2008 Essential Training

74 video lessons · 36228 viewers

Simon Allardice
Author

 
Expand all | Collapse all
  1. 2m 21s
    1. Welcome
      1m 19s
    2. Using the exercise files
      1m 2s
  2. 17m 58s
    1. SQL Server core concepts
      9m 4s
    2. SQL Server editions
      3m 8s
    3. Applications included with SQL Server
      5m 46s
  3. 26m 1s
    1. Preparing for installation
      3m 44s
    2. Creating service accounts
      2m 33s
    3. Installing SQL Server
      11m 42s
    4. Post-installation checks
      3m 9s
    5. Installing sample databases
      4m 53s
  4. 13m 35s
    1. Introduction to SQL Server Management Studio
      8m 7s
    2. Introduction to SQL Server Books Online
      3m 6s
    3. SQL Server system databases
      2m 22s
  5. 1h 26m
    1. Planning your database
      9m 39s
    2. Creating a SQL Server database
      4m 7s
    3. Creating tables
      7m 51s
    4. Data types in SQL Server
      12m 25s
    5. Defining keys
      8m 9s
    6. Creating default values
      4m 39s
    7. Creating check constraints
      2m 25s
    8. Creating unique constraints
      4m 34s
    9. Introduction to relationships and foreign keys
      9m 51s
    10. Creating relationships in SQL Server Management Studio
      8m 14s
    11. Database normalization
      11m 47s
    12. Creating computed columns
      3m 10s
  6. 23m 11s
    1. Using the SQL Server Import and Export Wizard
      3m 58s
    2. Importing Excel files into SQL Server
      6m 11s
    3. Importing CSV files into SQL Server
      5m 27s
    4. Importing Access databases into SQL Server
      7m 35s
  7. 55m 29s
    1. Introduction to Transact-SQL
      3m 43s
    2. Using SELECT statements
      7m 16s
    3. Changing the default database
      2m 21s
    4. Creating conditions in SQL
      8m 10s
    5. Sorting your output
      3m 23s
    6. Using aggregate functions
      7m 12s
    7. Finding unique values
      2m 14s
    8. Joining multiple tables together
      8m 0s
    9. Using subqueries
      9m 33s
    10. Viewing execution plans
      3m 37s
  8. 19m 36s
    1. Writing INSERT statements
      5m 47s
    2. Writing UPDATE statements
      4m 38s
    3. Writing DELETE statements
      2m 54s
    4. Using the OUTPUT clause to return inserted keys and GUIDs
      6m 17s
  9. 32m 52s
    1. Introduction to SQL functions
      6m 26s
    2. Using SQL configuration functions
      2m 14s
    3. Using string functions
      7m 26s
    4. Using date functions
      6m 27s
    5. Creating user-defined functions
      10m 19s
  10. 28m 46s
    1. Introduction to stored procedures
      4m 23s
    2. Creating stored procedures
      11m 23s
    3. Introducing transactions
      4m 23s
    4. Creating transactions
      8m 37s
  11. 16m 39s
    1. Understanding and creating indexes
      6m 32s
    2. Monitoring and rebuilding indexes
      6m 0s
    3. Monitoring database size and integrity
      4m 7s
  12. 11m 41s
    1. Creating backups
      4m 21s
    2. Creating differential backups and using backup compression
      3m 40s
    3. Restoring databases
      3m 40s
  13. 17m 40s
    1. Introduction to SQL Server security and permissions
      5m 54s
    2. Adding a Windows user to the database
      5m 7s
    3. Creating SQL Server logins and switching authentication modes
      6m 39s
  14. 36m 41s
    1. Introduction to SQL Server Reporting Services
      2m 52s
    2. Connecting to the Report Manager
      4m 29s
    3. Using Report Builder
      12m 4s
    4. Formatting values in reports
      4m 17s
    5. Adding indicators to reports
      5m 11s
    6. Adding charts to reports
      3m 54s
    7. Working with report security
      3m 54s
  15. 24m 41s
    1. Introduction to SQL Server Integration Services (SSIS)
      1m 57s
    2. Using Business Intelligence Development Studio (BIDS)
      6m 59s
    3. Creating and executing a simple SSIS package
      7m 35s
    4. Importing packages into SQL Server Management Studio
      3m 21s
    5. Scheduling jobs with SQL Server Agent
      4m 49s
  16. 31s
    1. Goodbye
      31s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed SQL Server 2008 Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.