时间差异(月):
select (date '2003-08-15' - date '2003-01-01') month;
时间差异(天):
select date '2003-08-15' - date '2003-01-01'; [@more@]
时间差异(秒):
select ((time02 / 10000) * 3600 + (time02 / 100 MOD 100) * 60 + (time02 MOD 100)) - ((time01 / 10000) * 3600 + (time01 / 100 MOD 100) * 60 + (time01 MOD 100)) as time_diff
月份操作:
select add_months(current_date, 3);
ADD_MONTHS(Date, -2)
add_months(date '2002-01-31', 1);
本月第一天:
select date - extract(day from date) + 1
上月最后一天:
select date '2008-03-03' - extract(day from '2008-03-03')
decemal转成time:
select cast(cast(cast(TimeFld as format '99:99:99.99') as char(11)) as time(6)) from DBC.EventLog
依照当前时间得到timestamp:
select cast(current_date as timestamp(2)) + ((current_time - time '00:00:00') hour to second);
上个星期五:
select *
from sys_calendar.calendar
where day_of_week = 6 --星期五
and calendar_date between date -6 and date; --上个星期
上周的今天:
select * from sys_calendar.calendar where calendar_date = date-7
日期存储
日期使用以下公式在内部存储为整数。
((YEAR - 1900) * 10000) + (MONTH * 100) + DAY
您可以使用以下查询来检查日期的存储方式。
SELECT CAST(CURRENT_DATE AS INTEGER);
由于日期存储为整数,您可以对它们执行一些算术运算。 Teradata提供执行这些操作的函数。
提取
EXTRACT函数从DATE值提取日,月和年的部分。 此功能还用于从TIME / TIMESTAMP值提取小时,分钟和秒。例
以下示例显示如何从日期和时间戳值提取年,月,日期,小时,分钟和秒值。SELECT EXTRACT(YEAR FROM CURRENT_DATE);
EXTRACT(YEAR FROM Date) ----------------------- 2016 SELECT EXTRACT(MONTH FROM CURRENT_DATE); EXTRACT(MONTH FROM Date) ------------------------ 1 SELECT EXTRACT(DAY FROM CURRENT_DATE); EXTRACT(DAY FROM Date) ------------------------ 1 SELECT EXTRACT(HOUR FROM CURRENT_TIMESTAMP); EXTRACT(HOUR FROM Current TimeStamp(6)) --------------------------------------- 4 SELECT EXTRACT(MINUTE FROM CURRENT_TIMESTAMP); EXTRACT(MINUTE FROM Current TimeStamp(6)) ----------------------------------------- 54 SELECT EXTRACT(SECOND FROM CURRENT_TIMESTAMP); EXTRACT(SECOND FROM Current TimeStamp(6)) ----------------------------------------- 27.140000
间隔
Teradata提供INTERVAL函数以对DATE和TIME值执行算术运算。 有两种类型的INTERVAL函数。年 - 月间隔
- YEAR
- YEAR TO MONTH
- MONTH
日间隔
- DAY
- DAY TO HOUR
- DAY TO MINUTE
- DAY TO SECOND
- HOUR
- HOUR TO MINUTE
- HOUR TO SECOND
- MINUTE
- MINUTE TO SECOND
- SECOND
例子
以下示例将3年添加到当前日期。
SELECT CURRENT_DATE, CURRENT_DATE + INTERVAL '03' YEAR; Date (Date+ 3) -------- --------- 16/01/01 19/01/01
以下示例将3年和01个月添加到当前日期。
SELECT CURRENT_DATE, CURRENT_DATE + INTERVAL '03-01' YEAR TO MONTH; Date (Date+ 3-01) -------- ------------ 16/01/01 19/02/01
以下示例将01天,05小时和10分钟添加到当前时间戳。
SELECT CURRENT_TIMESTAMP,CURRENT_TIMESTAMP + INTERVAL '01 05:10' DAY TO MINUTE; Current TimeStamp(6) (Current TimeStamp(6)+ 1 05:10) -------------------------------- -------------------------------- 2016-01-01 04:57:26.360000+00:00 2016-01-02 10:07:26.360000+00:00
实战一: 将整数型转换为日期格式或时间格式
select REPORT_DATE, Cast(Trim(REPORT_DATE) AS DATE Format 'YYYYMMDD'), CAST((CAST(REPORT_DATE AS DATE FORMAT 'YYYYMMDD')(format 'yyyy-mm-dd')(char(10)))|| ' 00:00:00' as timestamp(0)) from
运行结果:
实战二:将时间转换成日期
SELECT T1.DT_FWD, CAST(CAST((T1.DT_FWD (FORMAT 'YYYYMMDD')) AS VARCHAR(26)) AS TIMESTAMP FORMAT'YYYYMMDD') AS DT_VIEW_D, CAST(CAST((T1.DT_FWD (FORMAT 'YYYYMMDDHH')) AS VARCHAR(26)) AS TIMESTAMP FORMAT'YYYYMMDDHH') AS DT_VIEW_H, CAST(CAST((T1.DT_FWD (FORMAT 'YYYYMMDDHHMI')) AS VARCHAR(26)) AS TIMESTAMP FORMAT'YYYYMMDDHHMI') AS DT_VIEW_HM FROM xxx T1
运行结果: