|
Using Date and Time Values
|
|
|
Instead of using only the time or only a date value,
you may want to use theic combinaion. You have two options. You can
use two separate variables or columns, or you can combine both values in one
unit.
We already know how to declare a date variable,
how to create a date-based column, how to declare a time variable,
and how to create a time-based column. In the same way, you can
create a table that uses one or many combinations of date-based
columns and time-based columns. After creating a column, you can
add the records as necessary.
Data analysis on date-based columns and
time-based columns follows the descriptions with which we are
already familiar.
If you sort records on a table that uses
combinations of date and time columns:
|
 |
- If you sort the records on the date-based columns only, they
would be sorted in chronological order regardless of the
chronology of the time-based columns
- If you sort the records on the time-based columns only, they
would be sorted in chronological order regardless of the
chronology of the date-based columns
- If you sort the records using a combination of a date-based
column followed by a time-based column, the records would first
be grouped by the dates that occur together. Then, inside of the
group, the records would be sorted based on the chronology of
time
- If you sort the records using a combination of a time-based
column followed by a date-based column, the records would first
be grouped by the times that occur together. Then, inside of the
group, the records would be sorted based on the chronology of date
|
Practical
Learning: Using a Combination of Date/Time Columns
|
|
- In the Object Explorer, right-click Academics.UndergraduateSchedules
and click Delete
- Click inside the Query Editor and press Ctrl + A
- To create a table, type the following:
CREATE TABLE Academics.UndergraduateSchedules
(
Semester nvarchar(20),
StartDate date,
EndDate date,
StartTime time,
EndTime time,
Weekdays nvarchar(32),
CourseCode nvarchar(10)
);
GO
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SPRING 2012', N'20120109', N'20120318', N'08:15', N'10:10', N'M-W', N'WRTG 101');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SPRING 2012', N'20120109', N'20120318', N'10:25', N'12:20', N'M-W', N'WRTG 101');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SPRING 2012', N'20120109', N'20120318', N'15:35', N'17:30', N'M-W', N'WRTG 101');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SPRING 2012', N'20120109', N'20120226', N'10:25', N'12:20', N'M-W-F', N'WRTG 101');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SPRING 2012', N'20120109', N'20120226', N'08:15', N'10:10', N'M-W-F', N'WRTG 101');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SPRING 2012', N'20120109', N'20120226', N'10:25', N'12:20', N'M-W-F', N'WRTG 101');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SPRING 2012', N'20120109', N'20120318', N'10:25', N'12:20', N'M-W', N'MATH 106');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SPRING 2012', N'20120109', N'20120318', N'08:15', N'10:10', N'T-H', N'MATH 106');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SPRING 2012', N'20120109', N'20120318', N'13:25', N'15:30', N'T-H', N'EDPD 100');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SPRING 2012', N'20120109', N'20120318', N'15:35', N'17:30', N'T-H', N'EDPD 100');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SPRING 2012', N'20120109', N'20120318', N'13:25', N'15:30', N'M-W', N'EDPD 100');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SPRING 2012', N'20120109', N'20120318', N'15:35', N'17:30', N'M-W', N'EDPD 100');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SUMMER 2012', N'20120514', N'20120819', N'08:15', N'10:10', N'M-W', N'WRTG 101');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SUMMER 2012', N'20120514', N'20120819', N'10:25', N'12:20', N'M-W', N'EDPD 100');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SUMMER 2012', N'20120514', N'20120819', N'08:15', N'10:10', N'T-H', N'MATH 106');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SUMMER 2012', N'20120514', N'20120819', N'13:25', N'15:20', N'M-W', N'MATH 115');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SUMMER 2012', N'20120514', N'20120819', N'08:15', N'10:10', N'T-H', N'HIST 140');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SUMMER 2012', N'20120514', N'20120819', N'10:25', N'12:20', N'T-H', N'HIST 140');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SUMMER 2012', N'20120514', N'20120708', N'08:15', N'10:10', N'M-W', N'SOCY 100');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SUMMER 2012', N'20120514', N'20120708', N'10:25', N'12:20', N'M-W', N'SOCY 100');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SUMMER 2012', N'20120514', N'20120708', N'08:15', N'10:10', N'T-H', N'SOCY 100');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SUMMER 2012', N'20120514', N'20120708', N'10:25', N'12:20', N'T-H', N'SOCY 100');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SUMMER 2012', N'20120514', N'20120819', N'15:35', N'17:30', N'T-H', N'HIST 215');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SUMMER 2012', N'20120514', N'20120819', N'13:25', N'15:20', N'T-H', N'HIST 215');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SUMMER 2012', N'20120514', N'20120819', N'08:15', N'10:10', N'T-H', N'FINA 101');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SUMMER 2012', N'20120514', N'20120819', N'10:25', N'12:20', N'M-W-F', N'GVPS 140');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SUMMER 2012', N'20120514', N'20120819', N'10:25', N'12:20', N'T-H', N'HIST 216');
INSERT INTO Academics.UndergraduateSchedules(Semester, StartDate, EndDate, StartTime, EndTime, Weekdays, CourseCode) VALUES(N'SUMMER 2012', N'20120514', N'20120819', N'08:15', N'10:10', N'M-W', N'ACCT 311');
GO
- Press F5 to execute
- Click inside the Query Editor and press Ctrl + A
- To sort the records based on a date field, type the following code:
SELECT Semester,
StartDate "Start Date",
EndDate "End Date",
StartTime "Start Time",
EndTime "End Time",
Weekdays,
CourseCode "Course Code"
FROM Academics.UndergraduateSchedules
ORDER BY StartDate;
GO
- To execute the statement, press F5
- To sort the records based on a date field, change the statement as
follows:
SELECT Semester,
StartDate "Start Date",
EndDate "End Date",
StartTime "Start Time",
EndTime "End Time",
Weekdays,
CourseCode "Course Code"
FROM Academics.UndergraduateSchedules
ORDER BY StartTime;
GO
- To execute the statement, press F5
- To sort records based on a date-based field followed by a time-based
field, change the statement as follows:
SELECT Semester,
StartDate "Start Date",
EndDate "End Date",
StartTime "Start Time",
EndTime "End Time",
Weekdays,
CourseCode "Course Code"
FROM Academics.UndergraduateSchedules
ORDER BY StartDate, StartTime;
GO
- To execute the statement, press F5
- To sort records based on a time-based field followed by a date-based
field, change the statement as follows:
SELECT Semester,
StartTime "Start Time",
EndTime "End Time",
StartDate "Start Date",
EndDate "End Date",
Weekdays,
CourseCode "Course Code"
FROM Academics.UndergraduateSchedules
ORDER BY StartDate, StartTime;
GO
- To execute the statement, press F5
So far, we have used date values and time values in
separate variables and columns. Transact-SQL gives you the ability to
combine a date and time values into one unit. To support this, it provides
the DATETIME2 data type. This data type counts dates from January
1st, 0001 and ends on December 31st, 9999. Probably the biggest feature of
this data type is that it is a combination of the date and the
time data types. This means that everything we have seen about both
types is available here.
To declare a variable that can combine date and time
values, use the DATETIME2 data type. In the same way, you can create
a column of a table and apply this data type to it (the column). To initialize
the variable or assign a value to the column, you can use any of the
formulas we saw for dates or for times, or combine those formulas. This
means that you can use one of the following formulas:
YYYYMMDD
YYYYMMDD hh:mm:ss
YYYYMMDD hh:mm:ss[.fractional seconds]
YYYY-MM-DD
YYYY-MM-DD hh:mm:ss
YYYY-MM-DD hh:mm:ss[.fractional seconds]
MM-DD-YY
MM-DD-YY hh:mm:ss
MM-DD-YY hh:mm:ss[.fractional seconds]
MM-DD-YYYY
MM-DD-YYYY hh:mm:ss
MM-DD-YYYY hh:mm:ss[.fractional seconds]
MM/DD/YY
MM/DD/YY hh:mm:ss
MM/DD/YY hh:mm:ss[.fractional seconds]
MM/DD/YYYY
MM/DD/YYYY hh:mm:ss
MM/DD/YYYY hh:mm:ss[.fractional seconds]
Remember to include the value in single-quotes. Other
than that, all the ways we saw to specify the value of a date or of a time
can be used with the DATETIME2 data type. As mentioned already, you
can also combine the values. Here are examples:
DECLARE @FullName nvarchar(60),
@DateOfBirth date,
@DateRegistered datetime2
SET @FullName = N'John Summons';
SET @DateOfBirth = N'19960426';
SET @DateRegistered = N'20090629';
SELECT @FullName AS [Full Name],
@DateOfBirth AS [Date of Birth],
@DateRegistered AS [Date Registered];
SET @FullName = N'James Haans';
SET @DateOfBirth = N'1994-10-25';
SET @DateRegistered = N'2009-08-02';
SELECT @FullName AS [Full Name],
@DateOfBirth AS [Date of Birth],
@DateRegistered AS [Date Registered];
SET @FullName = N'Gertrude Monay';
SET @DateOfBirth = N'06-16-92';
SET @DateRegistered = N'2009-12-24 12:36';
SELECT @FullName AS [Full Name],
@DateOfBirth AS [Date of Birth],
@DateRegistered AS [Date Registered];
SET @FullName = N'Philomène Guillon';
SET @DateOfBirth = N'1996-10-16';
SET @DateRegistered = N'10/14/08 09:42:05.136';
SELECT @FullName AS [Full Name],
@DateOfBirth AS [Date of Birth],
@DateRegistered AS [Date Registered];
SET @FullName = N'Eddie Monsoon';
SET @DateOfBirth = N'08/10/96';
SET @DateRegistered = N'2009-06-02 12:36';
SELECT @FullName AS [Full Name],
@DateOfBirth AS [Date of Birth],
@DateRegistered AS [Date Registered];
SET @FullName = N'Peter Mukoko';
SET @DateOfBirth = N'03-10-1994';
SET @DateRegistered = N'7/22/2009 10:24:46.248';
SELECT @FullName AS [Full Name],
@DateOfBirth AS [Date of Birth],
@DateRegistered AS [Date Registered];
SET @FullName = N'Chritian Allen';
SET @DateOfBirth = N'06/16/1995';
SET @DateRegistered = N'02-09-2009 12:36';
SELECT @FullName AS [Full Name],
@DateOfBirth AS [Date of Birth],
@DateRegistered AS [Date Registered];
GO
Besides the DATE, the TIME, and the
DATETIME2 data types, Transact-SQL supports the smalldatetime and
the datetime data types. These are old data types. Although still
available, they are kept for backward compatibility and you should avoid
using them.
One of the ways you can create a value that is a
combination of date and time is by using a function named
DATETIME2FROMPARTS. Its syntax is:
DATETIME2FROMPARTS(int year,
int month,
int day,
int hour,
int minute,
int seconds,
int fractions,
int precision) RETURNS datetime2;
As you can see, this function takes 8 required
arguments. As mentioned already, the datetime2 is a combination of
date and time. Based on this, the first 3 arguments are the same used in the
DATEFROMPARTS() function. The other arguments are the same used in
the TIMEFROMPARTS() Function.
|
Data Analysis On Date/Time Values
|
|
|
Sorting Date and Time-Based Fields
|
|
If you sort records based on a column that uses a
combination of a date and a time values, if the column has null fields, the
records with null would display first. If the values are combinations of date
and time values, the date values would first be sorted in chronological order. If
some records occur on the same day but at different times, the records
with similar dates would be grouped first. Then inside a group, the records woulds be
sorted by time. Consider the following example:
USE master;
GO
CREATE DATABASE IceCreamFactory1;
GO
USE IceCreamFactory1;
GO
CREATE SCHEMA IceCream;
GO
CREATE TABLE IceCream.Orders
(
OrderID int identity(1, 1) not null,
OrderedPeriod DateTime2 null
);
GO
INSERT IceCream.Orders(OrderedPeriod)
VALUES(N'2012-02-14 10:12'), (N'2012-02-15 09:08'),
(N'2012-05-10 15:24'), (N'2012-07-04 14:01'),
(N'2012-04-18 19:16'), (N'2012-04-18 09:15'),
(N'2012-04-18 12:48'), (N'2012-07-04 11:26');
GO
SELECT OrderID, OrderedPeriod FROM IceCream.Orders
ORDER BY OrderedPeriod;
GO
This would produce

|
Logical Comparisons on Date/Time Fields
|
|
You can perform all types of logical comparisons on
variables or columns that use the datetime2 data type. The value may
have a date only, a time only, or a combination of date and time. If
the value has only a date, the comparison follows the description we saw for
the date data type. If the value has only a time, the comparison
follows the rules for the time data type. If the value uses a
combination of date and time:
- If you compare the values for equality, =, both must occur on the
same day and the same time to produce a true value. Here is an example:
USE master;
GO
DECLARE @FirstOrder datetime2,
@SecondOrder datetime2;
SET @FirstOrder = N'2012-02-14 10:12:00';
SET @SecondOrder = N'2012-02-14 10:12:00';
IF @FirstOrder = @SecondOrder
PRINT N'Both orders were made at the same time.';
ELSE
PRINT N'The orders were made at different times.';
GO
This would produce

If there is a slight difference in the value, even for one second, the
equality comparison produces a false value:

- If you perform a comparison for different values, <>, any difference
in the values would produce a true result
- To find out if one period occurs BEFORE the other, you compare using
the Less Than < operator. If the first value occurs before the second,
the comparison produces true. If the date parts are equal, the first
time period must occur before the second. Here is an example:
USE master;
GO
DECLARE @FirstOrder datetime2,
@SecondOrder datetime2;
SET @FirstOrder = N'2012-02-14 10:12:00';
SET @SecondOrder = N'2012-02-14 10:12:01';
IF @FirstOrder < @SecondOrder
PRINT N'The first order was placed before the second.';
ELSE
PRINT N'Either both orders were made at the same time or the first was after the second.';
GO
- If you want to know whether both values occurred at the same time OR
the first occurred BEFORE the second, use the Less Than Or Equal
operator <=. An alternative is to use the Not Greater Than operator, !>,
to perform the same comparison
- To find out if one value occurred after the other, perform the
comparison using >. In this case, if the first value is chronologically
higher than the second, the comparison produces true
- If you want to know whether both values occurred at the same time OR
the first occurred AFTER the second, use the Greater Than Or Equal
operator >=. You can also use the Not Less Than operator, !<, to perform
the same comparison
|
Practical
Learning: Ending the Lesson
|
|
- Close Microsoft SQL Server
- If asked whether you want to save the file, click No
|
|