Tech Tip: Calculate Time Difference in SQL
PRODUCT: 4D | VERSION: 14.x | PLATFORM: Mac & Win
Published On: October 28, 2016
In 4D, the SQL engine does not allow athrimetic calculations between two time types.
For the following table:
The following code will generate an error
ARRAY TIME(Times;0) Begin SQL SELECT (Tend-Tstart) FROM TimeTest Into :Times End SQL |
To work around this a simple method can be created and ran inside the SQL Statment.
// Util_SQL_TIMEDIFF // // Desription: // Used For SQL calculation of Diffiference between two times // // Parameters: // $1: End Time (Larger Value of the Two) // $2: Start Time (Lesser Value of the Two) // Result: // $0: Time difference between end and start times // ---------------------------------------------------- C_TIME($1;$end_t) C_TIME($2;$start_t) C_TIME($0;$dif_t) If (Count parameters=2) $end_t:=$1 $start_t:=$2 $dif_t:=$end_t-$start_t $0:=$dif_t End if |
The "Available through 4D Mobile call" Method Properrty must be toggled on:
Then the method can be used in the SQL statement as follows:
ARRAY TIME(Times;0) Begin SQL SELECT {FN Util_SQL_TIMEDIFF(Tend,Tstart) AS DURATION} FROM TimeTest Into :Times End SQL |