
Caché SQL Reference
TRUNCATE



Server:docs2 
Instance:LATEST 
User:UnknownUser 


A scalar numeric function that truncates a number at a specified number of digits.
Synopsis
{fn TRUNCATE(numericexpr,scale)}
TRUNCATE truncates
numericexpr by truncating at the
scale number of digits from the decimal point. It does not round numbers or add padding zeroes. Leading and trailing zeroes are removed before the
TRUNCATE operation.
TRUNCATE returns the same data type as
numericexpr.

If
scale is a positive number, truncation is performed at that number of digits to the right of the decimal point. If
scale is equal to or larger than the number of decimal digits, no truncation or zero filling occurs.

If
scale is zero, the number is truncated to a whole integer. In other words, truncation is performed at zero digits to the right of the decimal point; all decimal digits and the decimal point itself are truncated.

If
scale is a negative number, truncation is performed at that number of digits to the left of the decimal point. If
scale is equal to or larger than the number of integer digits in the number, zero is returned.

If
numericexpr is zero (however expressed: 00.00, 0, etc.)
TRUNCATE returns 0 (zero) with no decimal digits, regardless of the
scale value.

TRUNCATE and
ROUND are numeric functions that perform similar operations; they both can be used to decrease the number of significant decimal or integer digits of a number. However,
TRUNCATE does not perform rounding.
TRIM can be used to perform a similar truncation operation on strings.
TRUNCATE can only be used as an ODBC scalar function (with the curly brace syntax).
The following two examples both truncate a number to two decimal digits. The first (using Dynamic SQL) specifies
scale as an integer; the second (using Embedded SQL) specifies
scale as a host variable that resolves to an integer:
SET myquery = "SELECT {fn TRUNCATE(654.321888,2)} AS trunc"
SET tStatement = ##class(%SQL.Statement).%New()
SET qStatus = tStatement.%Prepare(myquery)
SET rset = tStatement.%Execute()
DO rset.%Display()
SET x=2
&sql(SELECT {fn TRUNCATE(654.321888,:x)}
INTO :a)
IF SQLCODE'=0 {
WRITE !,"Error code ",SQLCODE }
ELSE {
WRITE !,"truncated value is: ",a }
both examples return 654.32 (truncation to two decimal places).
The following Dynamic SQL example specifies a
scale larger than the number of decimal digits:
SET myquery = "SELECT {fn TRUNCATE(654.321000,9)} AS trunc"
SET tStatement = ##class(%SQL.Statement).%New()
SET qStatus = tStatement.%Prepare(myquery)
SET rset = tStatement.%Execute()
DO rset.%Display()
it returns 654.321 (Caché removed the trailing zeroes before the truncation operation; no truncation or zero padding occurred).
The following Dynamic SQL example specifies a
scale of zero:
SET myquery = "SELECT {fn TRUNCATE(654.321888,0)} AS trunc"
SET tStatement = ##class(%SQL.Statement).%New()
SET qStatus = tStatement.%Prepare(myquery)
SET rset = tStatement.%Execute()
DO rset.%Display()
it returns 654 (all decimal digits and the decimal point are truncated).
The following Dynamic SQL example specifies a negative
scale:
SET myquery = "SELECT {fn TRUNCATE(654.321888,2)} AS trunc"
SET tStatement = ##class(%SQL.Statement).%New()
SET qStatus = tStatement.%Prepare(myquery)
SET rset = tStatement.%Execute()
DO rset.%Display()
it returns 600 (two integer digits have been truncated and replaced by zeroes; note that no rounding has been done).
The following Dynamic SQL example specifies a negative
scale as large as the integer portion of the number:
SET myquery = "SELECT {fn TRUNCATE(654.321888,3)} AS trunc"
SET tStatement = ##class(%SQL.Statement).%New()
SET qStatus = tStatement.%Prepare(myquery)
SET rset = tStatement.%Execute()
DO rset.%Display()