我正在使用Advantage sql数据库,并尝试减去2个日期以获取天数。我以为在ADV SQL中,您可以减去日期,就可以计算出天数。
((已完成)-(开始日期))
1-23-19-1-3-19 = 20天
答案 0 :(得分:1)
如果两者均为DATE
类型,则可以直接在ADS SQL中减去日期。如果它们都是TIMESTAMP
,则可以将TIMESTAMPDIFF()
与TSI_SQL_DAYS
用作间隔。如果一个是DATE
,另一个是TIMESTAMP
,则必须将TIMESTAMP
转换为DATE
,然后才能直接减去。
以下是所有这三种选择的示例。
将两列都设为DATE
类型:
create table Test (ID integer, StartDate Date, Completed Date);
insert into Test values (1, '2019-01-01', '2019-01-31');
insert into Test values (2, '2019-01-01', '2018-02-28');
select id, startdate, completed, completed - startdate as days from test;
输出:
id startdate completed days
--- ----------- ----------- ----
1 01/01/2019 01/31/2019 30
2 01/01/2019 02/28/2019 58
将两列都设为TIMESTAMP
类型:
create table Test2 (ID integer, StartDate TimeStamp, Completed TimeStamp);
insert into Test2 (ID, StartDate, Completed)
values (1, CreateTimeStamp(2019, 1, 1, 0, 0, 0, 1),
CreateTimeStamp(2019, 1, 31, 0, 0, 0, 1));
insert into Test2 (ID, StartDate, Completed)
values (2, CreateTimeStamp(2019, 1, 1, 0, 0, 0, 1),
CreateTimeStamp(2019, 2, 28, 0, 0, 0, 1));
select id, startdate, completed,
TimeStampDiff(SQL_TSI_DAY, StartDate, Completed) as days from test2;
输出:
id startdate completed days
--- ----------- ----------- ----
1 01/01/2019 01/31/2019 30
2 01/01/2019 02/28/2019 58
一个DATE
和一个TIMESTAMP
:
create table Test3 (ID integer, StartDate TimeStamp, Completed Date);
insert into Test3 (ID, StartDate, Completed)
values (1, CreateTimeStamp(2019, 1, 1, 0, 0, 0, 1), '2019-01-31');
insert into Test3 (ID, StartDate, Completed)
values (2, CreateTimeStamp(2019, 1, 1, 0, 0, 0, 1), '2019-02-28');
select id, startdate, completed,
Completed - Cast(StartDate AS SQL_DATE) as days from test3;
输出:
id startdate completed days
--- ----------- ----------- ----
1 01/01/2019 01/31/2019 30
2 01/01/2019 02/28/2019 58
答案 1 :(得分:0)
我认为timestampdiff()
应该做您想要做的事情:
timestampdiff(SQL_TSI_DAY, startdate, completed)
答案 2 :(得分:0)
这是一个非常不寻常的数据库,可引起您的疑问-文档建议(https://devzone.advantagedatabase.com/dz/webhelp/advantage10/index.html?master_supported_scalar_functions.htm)您需要:
TIMESTAMPDIFF(SQL_TSI_DAY,completed, startdate)
编辑:包括演员
TIMESTAMPDIFF(SQL_TSI_DAY,completed, cast(startdate as SQL_TIMESTAMP))
文档:
TIMESTAMPDIFF(interval,timestamp1,timestamp2)
基于减时间戳1返回整数间隔的数量 从时间戳记2。支持的时间间隔值为:SQL_TSI_FRAC_SECOND, SQL_TSI_SECOND,SQL_TSI_MINUTE,SQL_TSI_HOUR,SQL_TSI_DAY, SQL_TSI_WEEK,SQL_TSI_MONTH,SQL_TSI_QUARTER,SQL_TSI_YEAR。