Sams Teach Yourself SQL in 24 Hours
- Table of Contents
- Copyright
- About the Authors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- Part I: A SQL Concepts Overview
- Hour 1. Welcome to the World of SQL
- Part II: Building Your Database
- Hour 2. Defining Data Structures
- Hour 3. Managing Database Objects
- Hour 4. The Normalization Process
- Hour 5. Manipulating Data
- Hour 6. Managing Database Transactions
- Part III: Getting Effective Results from Queries
- Hour 7. Introduction to the Database Query
- Hour 8. Using Operators to Categorize Data
- Hour 9. Summarizing Data Results from a Query
- Hour 10. Sorting and Grouping Data
- Hour 11. Restructuring the Appearance of Data
- Hour 12. Understanding Dates and Times
- How Is a Date Stored?
- Date Functions
- Date Conversions
- Summary
- Q&A
- Workshop
- Part IV: Building Sophisticated Database Queries
- Hour 13. Joining Tables in Queries
- Hour 14. Using Subqueries to Define Unknown Data
- Hour 15. Combining Multiple Queries into One
- Part V: SQL Performance Tuning
- Hour 16. Using Indexes to Improve Performance
- Hour 17. Improving Database Performance
- Part VI: Using SQL to Manage Users and Security
- Hour 18. Managing Database Users
- Hour 19. Managing Database Security
- Part VII: Summarized Data Structures
- Hour 20. Creating and Using Views and Synonyms
- Hour 21. Working with the System Catalog
- Part VIII: Applying SQL Fundamentals in Today's World
- Hour 22. Advanced SQL Topics
- Hour 23. Extending SQL to the Enterprise, the Internet, and the Intranet
- Hour 24. Extensions to Standard SQL
- Part IX: Appendixes
- Appendix A. Common SQL Commands
- Appendix B. Using MySQL for Exercises
- Appendix C. Answers to Quizzes and Exercises
- Appendix D. CREATE TABLE Statements for Book Examples
- Appendix E. INSERT Statements for Data in Book Examples
- Appendix F. Glossary
- Appendix G. Bonus Exercises
How Is a Date Stored?
Each implementation has a default storage format for the date and time. This default storage often varies among different implementations, as do other data types for each implementation. The following sections begin by reviewing the standard format of the DATETIME data type and its elements. Then you see the data types for date and time in some popular implementations of SQL, including Oracle, Sybase, and Microsoft SQL Server.
Standard Data Types for Date and Time
There are three standard SQL data types for date and time (DATETIME) storage:
|
Data Type |
Usage |
|
DATE |
Stores date literals |
|
TIME |
Stores time literals |
|
TIMESTAMP |
Stores date and time literals |
Format and range of valid values for each data type:
|
DATE |
|
Format: YYYY-MM-DD |
|
Range: 0001-01-01 to 9999-12-31 |
|
TIME |
|
Format: HH:MI:SS. nn ... |
|
Range: 00:00:00... to 23:59:61.999... |
|
TIMESTAMP |
|
Format: YYYY-MM-DD HH:MI:SS. nn ... |
|
Range: 0001-01-01 00:00:00... to 9999-12-31 23:59:61.999... |
DATETIME Elements
DATETIME elements are those elements pertaining to date and time that are included as part of a DATETIME definition. The following is a list of the constrained DATETIME elements and a valid range of values for each element:
|
YEAR |
0001 to 9999 |
|
MONTH |
01 to 12 |
|
DAY |
01 to 31 |
|
HOUR |
00 to 23 |
|
MINUTE |
00 to 59 |
|
SECOND |
00.000... to 61.999... |
Seconds can be represented as a decimal, allowing the expression of tenths of a second, hundredths of a second, milliseconds, and so on. Each of these elements, except for the last, is self-explanatory; they are elements of time that we deal with on a daily basis. You may question the fact that a minute can contain more than 60 seconds. According to the ANSI standard, this 61.999 seconds is due to the possible insertion or omission of a leap second in a minute, which in itself is a rare occurrence. Refer to your implementation on the allowed values because date and time storage may vary widely.
Implementation-Specific Data Types
As with other data types, each implementation provides its own representation and syntax. This section shows how three products (Oracle, Sybase, and SQLBase) have been implemented with date and time.
|
Product |
Data Type |
Use |
|
Oracle |
DATE |
Stores both date and time information |
|
Sybase |
DATETIME |
Stores both date and time information |
|
SMALLDATETIME |
Stores both date and time information, but includes a smaller date range than DATETIME |
|
|
SQLBase |
DATETIME |
Stores both date and time information |
|
TIMESTAMP |
Stores both date and time information |
|
|
DATE |
Stores a date value |
|
|
TIME |
Stores a time value |
Date Functions | Next Section

Account Sign In
View your cart