Академический Документы
Профессиональный Документы
Культура Документы
This document
explains how to do various things that you may want to do.
Preliminaries
The examples below use Teradata syntax, so they assume that you are running in BTEQ.
If you are using SQL Assistant (Queryman), be sure to uncheck the option box "Allow
use of ODBC SQL Extensions in queries". Changing the Date Time Format in your ODBC
data source to AAA or IIA may also help.
datecol means a column defined as DATE.
Dates
Literals
Date literals can be specified in many ways:
-- in the same format as the column they are being compared to
Observe in the examples above that the word DATE must be used to specify the data
type. To get the system date, therefore, CURRENT_DATE is a better choice than DATE.
datecol - datecol will return the number of days between two dates.
/*** Last day of the month is still the last day ***/
select add_months(date '2002-01-31', 1);
ADD_MONTHS(2002-01-31, 1)
------------------------2002-02-28
select day_of_week
from sys_calendar.calendar
where calendar_date = date '2003-05-01';
day_of_week
----------5 [i.e. Thursday]
Times
Literals
The easiest way to specify a time literal is in ANSI form with a 24-hour clock:
time '15:09:17'
But in V2R5 you can use another format as long as you tell Teradata what it is:
Some Data Dictionary tables have time columns that are defined as FLOAT rather than
TIME. Heres how to convert them to TIMEs, believe it or not:
Time differences
First we shall address the case where your time data is defined as a number (FLOAT or
DECIMAL, perhaps) in hhmmss form.
If the earlier time could fall on one day and the later time on the next day, you may have
to add 86,400 (the number of seconds in one day) to the later time, like so:
select case
when time02 >= time01 then
((time02 / 10000) * 3600
(time02 / 100 MOD 100) *
(time02 MOD 100)) ((time01 / 10000) * 3600
(time01 / 100 MOD 100) *
(time01 MOD 100))
+
60 +
+
60 +
18094.
Next we consider the case where your time data is defined as TIME(n). The usual way to
take the difference of two times would be as follows:
The above result has a data type of INTERVAL. If, however, you want to compute the
difference in seconds, as above, you again have to split the times up:
38363
Timestamps
Literals
The easiest way to specify a timestamp literal is in ANSI form:
cast(Date_column as TimeStamp(6))
+ ((Time_column - time '00:00:00') hour to second(6))
You can also convert this interval to seconds or minutes like so:
sec_diff
----------------32073995.639649
min_diff
----------534566
If you just want the number of days between two timestamps and wish to ignore the time
portion, either of the following two techniques will work, but note the differences. The
first technique lops off the time portion of each timestamp, so it will be equivalent to
subtracting the two days; the result is an integer. The second will take the time portion
into account and return an interval, so it will not count periods of time less than 24 hours.
Thus, the result could be one less than with the first technique. If you use the second
technique, be sure to allow enough digits for DAY.