-2

I am working in advantage sql database, and trying to subtract 2 dates to get number of days. I thought in ADV SQL you could just subtract the dates and it would give you the num of days.

((completed) - (startdate))

1-23-19 - 1-3-19 = 20 days

Brian K
  • 87
  • 7
  • Are your date columns of the correct type or are they strings? – Joakim Danielson Jun 12 '19 at 12:48
  • They are date fields – Brian K Jun 12 '19 at 12:50
  • Completed is a timestamp startdate is a Date – Brian K Jun 12 '19 at 12:54
  • I don't know anything about Advantage but it would makes sense if you first would have to convert timestamp to Date before doing the subtraction – Joakim Danielson Jun 12 '19 at 12:55
  • it is the timestamp field causing the issue. I do not know how to convert timestamp to date field in ADV sql. Cast or convert do not work. I am new to ADV SQL and i do not care for it – Brian K Jun 12 '19 at 13:09
  • What happens if you do `TIMESTAMPDIFF(SQL_TSI_DAY, CONVERT(startdate, SQL_TIMESTAMP), completed)` ? or maybe with cast insted of convert, `CAST( startdate AS SQL_TIMESTAMP)`? – Joakim Danielson Jun 12 '19 at 13:22
  • I get an error message. cast(startdate was SQL_timestamp) works, but thats not what i need i need to take completed(currently a timestamp) and make that date, and that doesnt work in the example you gave me. – Brian K Jun 12 '19 at 13:30
  • Are you sure about that if you want to use TIMESTAMPDIFF? Subtracting dates is probably not working – Joakim Danielson Jun 12 '19 at 13:32

3 Answers3

2

You can directly subtract dates in ADS SQL if both are of type DATE. If they're both TIMESTAMP, you can use TIMESTAMPDIFF() with TSI_SQL_DAYS as the interval. If one is a DATE and the other a TIMESTAMP, you have to convert the TIMESTAMP to a DATE, and then you can directly subtract.

Here are examples of all three of those alternatives.

With both columns as type 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;

Output:

id  startdate    completed    days      
--- -----------  -----------  ----
1   01/01/2019   01/31/2019     30
2   01/01/2019   02/28/2019     58

With both columns as type 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;

Output:

id  startdate    completed    days      
--- -----------  -----------  ----
  1 01/01/2019   01/31/2019     30
  2 01/01/2019   02/28/2019     58

With one a DATE and one a 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;

Output:

id  startdate    completed    days      
--- -----------  -----------  ----
  1 01/01/2019   01/31/2019     30
  2 01/01/2019   02/28/2019     58
Ken White
  • 123,280
  • 14
  • 225
  • 444
0

I think timestampdiff() should do what you want:

timestampdiff(SQL_TSI_DAY, startdate, completed)
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

This is a pretty unusual DB to get questions on - docs suggest (https://devzone.advantagedatabase.com/dz/webhelp/advantage10/index.html?master_supported_scalar_functions.htm ) that you need :

TIMESTAMPDIFF(SQL_TSI_DAY,completed, startdate)

Edit : including the cast

TIMESTAMPDIFF(SQL_TSI_DAY,completed, cast(startdate as SQL_TIMESTAMP))

Docs :

TIMESTAMPDIFF( interval, timestamp1, timestamp2 )

Returns number of integer intervals based on subtracting timestamp1 from timestamp2. Interval values supported are: 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.

Andrew
  • 26,629
  • 5
  • 63
  • 86
  • i get this error message when using your code. [SAP][Advantage SQL Engine]Invalid argument to scalar function: TIMESTAMPDIFF. Argument must be char, date, time or timestamp. -- Location of error in the SQL statement is: 232 (line: 6 column: 5) – Brian K Jun 12 '19 at 13:14
  • The docs show `cast` is supported, so try cast(yourDateField as SQL_TIMESTAMP) – Andrew Jun 12 '19 at 13:25
  • poQuery: Error 7200: AQE Error: State = S0000; NativeError = 2159; [SAP][Advantage SQL Engine]Invalid argument to scalar function: TIMESTAMPDIFF. Argument must be char, date, time or timestamp. -- Location of error in the SQL statement is: 291 (line: 7 column: 5) – Brian K Jun 12 '19 at 13:31
  • @BrianK - I would be tempted to cast both sides and see what it does - its quite possible that the types are not what you expect. – Andrew Jun 12 '19 at 13:35
  • @BrianK, it is PROBABLY due to your date references.. Are you dealing with actual date/time fields? Coming from a string provided input? if string format it should be presented in 'yyyy-MM-dd' format and not MM-dd-yy per your example – DRapp Jun 13 '19 at 15:56