Although you can store dates and times in columns of type CHAR or VARCHAR, you will find it more convenient to use datetime columns instead. If you put dates and times into date-time columns, the DBMS will format the dates and times in a standard way when you display the contents of the columns as part of SELECT statements. More importantly, by using datetime columns, you will be able to use specialized date and time functions (such as INTERVAL and EXTRACT) to manipulate date and time data.
The SQL-92 standard specifies five datetime data types:
-
DATE. Uses 10 characters to store the four-digit year, two-digit month, and two-digit day values of the date in the format 2000-04-25. Because the DATE data type uses a four-digit year, you can use it to represent any date from the year 0001 through the year 9999. Thus, SQL will have a year 10K problem, but I, for one, will let future generations worry about it.
-
TIME. Uses eight characters, including the colons, to represent the two-digit hours, two-digits minutes, and two-digit seconds in the format 19:22:34. Because the SQL formats time using the 24-hour clock, 19:22:34 represent 22 minutes and 34 seconds past 7 P.M., whereas 07:22:34 represents 22 minutes and 34 seconds past 7 A.M. If you define a column as type TIME, the default is for the DBMS to display only whole seconds. However, you can tell the DBMS to store (and display) fractions of seconds by adding the precision you want to the TIME data type when using it to define a column of type TIME. For example, if you create a table with the SQL statement
CREATE TABLE time_table
(time_with_seconds TIME(3))the DBMS will store time data including up to three digits representing thousandths of seconds.
-
TIMESTAMP. Includes both date and time using 26 characters-10 characters to hold the date, followed by a space for separation, and then 15 characters to represent the time, including a default of fractions of seconds to six decimal places. Thus, if you create a table using
CREATE TABLE time_table
(timestamp_column TIMESTAMP,
timestamp_column_no_decimal TIMESTAMP (0))the DBMS will store the date and time in TIMESTAMP_COLUMN formatted as 2000-04-25 19:22:34.123456, and the date and time in TIMESTAMP_COLUMN_NO_DECIMAL formatted as 2000-04-25 19:25:34. (The number in parenthesis ( () ) after TIMESTAMP specifies the precision of the fractions of seconds portion of the time-0, in the example.)
-
TIME WITH TIME ZONE. Uses 14 characters to represent the time and the offset from Universal Coordinated Time (UTC)-eight characters to hold the time followed by the offset of the local time from (UTC)-formerly known as Greenwich Mean Time or GMT. Therefore, if you create a table using
CREATE TABLE time_table
(time_with_gmt TIME WITH TIME ZONE,
time_with_seconds_gmt TIME (4) WITH TIME ZONE)the DBMS will store the time in TIME_WITH_GMT formatted as 19:22:24-05:00, and in TIME_WITH_SECONDS_GMT formatted as 19:22:24.1234-05:00. (The (4) in the data type for the TIME_WITH_SECONDS_GMT column in the example represents the optional precision you can specify to represent the fractions of seconds in the time.)
-
TIMESTAMP WITH TIME ZONE. Uses 32 characters to represent the date, the time, and the offset from Universal Coordinated Time (UTC)-10 characters to hold the date, followed by a space for separation, and then 21 characters to represent the time, including a default of fractions of seconds given to six decimal places and the office from UTC (GMT). Thus, if you create a table using
CREATE TABLE time_table
(timestamp_column TIMESTAMP WITH TIME ZONE,
timestamp_no_dec TIMESTAMP(0)WITH TIME ZONE)the DBMS stores the date and time in TIMESTAMP_COLUMN formatted as 2000-04-25 19:22:34.123456+04:00 and in TIMESTAMP_NO_DEC using the format 2000-04-25 19:25:34+01:00 (The number in parenthesis ( () ) after TIMESTAMP specifies the precision of the fractions of seconds portion of the time-0, in the example.)
Unfortunately, not all DBMS products support all five of the standard SQL datetime data types. In fact, some DBMS products even use TIMESTAMP for purposes other than defining columns that hold date and time data. As such, check your system manual to see which of the SQL datetime data types your DBMS supports.
Don't be surprised to find that your system uses a nonstandard data type such as DATETIME (used by SQLBase, Sybase, and MS-SQL Server) to format columns that will hold dates and times.
If your system uses the DATETIME data type, you can define a column to hold date and time using an SQL statement similar to:
CREATE TABLE date_table
(date_time DATETIME)
To insert a date and time into a DATETIME column, enclose the date and time in single quotes using an INSERT statement similar to:
INSERT INTO date_table
VALUES ('04/25/2000 21:05:06:123')
If you are using MS-SQL Server and execute the SQL statement
SELECT * FROM date_table
the DBMS will display the value in the DATE_TIME column as: 2000-04-25 21:05:06.123.
MS-SQL Server lets you specify the date in the INSERT statement using any one of a variety of formats, including but not limited to:
-
Apr 25 2000
-
APR 25 2000
-
April 25, 2000
-
25 April 2000
-
2000 April 25
-
4/25/00
-
4-25-2000
-
4.25.2000
MS-SQL Server also gives you a number of ways to express the time you want to insert into a DATETIME column. Valid ways to express time include:
-
9:05:06:123pm
-
9:5:6:123pm
-
9:05pm
-
21:00
-
9pm
-
9PM
-
9:05
(Note that the last entry in this example ["9:05"] will insert 9:05am and not 9:05pm.) If you insert a date without a time, MS-SQL Server will append 00:00:00:000 to your date. Thus, the SQL statement
INSERT INTO date_table VALUES ("2000 Apr 25")
will set the value of DATE_TIME to 2000-04-10 00:00:00.000. (MS-SQL Server will replace the portion of the time you leave off with zeroes.)
If you insert only a time into a DATETIME column, MS-SQL Server will replace the omitted date with 01/01/1900.
No comments:
Post a Comment