This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
dev:sql:functions:trunc [2009/12/07 14:38] tdo created |
dev:sql:functions:trunc [2016/06/28 22:38] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
{{page>:top_add&nofooter&noeditbtn}} | {{page>:top_add&nofooter&noeditbtn}} | ||
- | <html><div align="center"><span style="color:red">DRAFT</span></div></html> | ||
- | ====== Omnidex SQL Function: $TRUNC ====== | ||
{{page>:sql_bar&nofooter&noeditbtn}} | {{page>:sql_bar&nofooter&noeditbtn}} | ||
+ | ====== Omnidex SQL Function: TRUNC ====== | ||
===== Description ===== | ===== Description ===== | ||
- | The $TRUNC function returns a numeric expression truncated to a specified number of digits to the right of the decimal point. | + | The TRUNC function returns a numeric expression truncated to a specified number of digits to the right of the decimal point. If the specified number of digits is positive and longer than the digits after the decimal point, 0 is added after the fraction digits. If the specified number of digits is negative and larger than or equal to the digits before the decimal point, 0 is returned. When a numeric expression is truncated, the trailing digits are simply dropped off. The return value is not rounded in any way. |
- | If the value specified by n is positive and longer than the digits after the decimal point, 0 is added after the fraction digits. | + | This function returns the same datatype as the passed parameter. |
- | If the value specified by n is negative and larger than or equal to the digits before the decimal point, 0 is returned. | + | ===== Syntax ===== |
- | When a numeric expression is truncated, the trailing digits are simply dropped off. The return value is not rounded in any way. | + | $ROUND(numeric_expression [, n] ) |
+ | == numeric_expression == | ||
+ | |||
+ | Required. Any expression that returns a numeric expression. | ||
- | ===== Syntax ===== | + | == n == |
- | TRUNC(expression [,n]) | + | |
+ | Optional. An integer //n// is an integer indicating the number of digits to the right of the decimal. The default is 0. | ||
- | expression | ||
- | Required. The numerical expression that is to be truncated. | ||
- | |||
- | n | ||
- | Optional. n is an integer indicating the number of digits to display after the decimal point. | ||
===== Example ===== | ===== Example ===== | ||
- | ==== Example 1 ==== | + | ==== Example 1: Column ==== |
- | $TRUNC(123.4545, 2) result: 123.45 | + | |
- | $TRUNC(123.4545, -2) result: 100 | + | <code> |
+ | |||
+ | > select latitude, trunc(latitude) from countries with opt=none; | ||
+ | |||
+ | LATITUDE TRUNC(COUNTRIES.LATITUDE) | ||
+ | -------------------------------- -------------------------------- | ||
+ | 42.500000 42.000000 | ||
+ | 24.000000 24.000000 | ||
+ | 33.000000 33.000000 | ||
+ | 17.049999 17.000000 | ||
+ | 18.209999 18.000000 | ||
+ | 41.000000 41.000000 | ||
+ | 40.000000 40.000000 | ||
+ | |||
+ | </code> | ||
+ | |||
+ | ==== Example 2: Column rounded to a set number of decimal places ==== | ||
+ | |||
+ | Note that floating point values cannot represent ever value, and the values shown below are as close as floating point values can represent. | ||
+ | |||
+ | <code> | ||
+ | > select latitude, trunc(latitude, 1) from countries with opt=none; | ||
+ | |||
+ | LATITUDE TRUNC(COUNTRIES.LATITUDE,1) | ||
+ | -------------------------------- -------------------------------- | ||
+ | 42.500000 42.500000 | ||
+ | 24.000000 24.000000 | ||
+ | 33.000000 33.000000 | ||
+ | 17.049999 17.000000 | ||
+ | 18.209999 18.200001 | ||
+ | 41.000000 41.000000 | ||
+ | 40.000000 40.000000 | ||
+ | </code> | ||
+ | |||
+ | ====== Additional Resources ====== | ||
+ | |||
+ | See also: | ||
+ | {{page>dev:sql:functions:see_also_math&noheader&nofooter&noeditbtn}} | ||
- | $TRUNC(123.9994, 3) result: 123.999 | ||
- | $TRUNC(123.9995, 3) result: 123.999 | ||
{{page>:bottom_add&nofooter&noeditbtn}} | {{page>:bottom_add&nofooter&noeditbtn}} | ||
+ |