SQL - Scalar functions - ROUND
The ROUND function returns a rounded value of a number or a datetime value.
The schema is SYSIBM. The SYSFUN version of the ROUND numeric function continues to be available.
The return value depends on the first argument:
If the expression is a decimal floating-point data type, the DECFLOAT rounding mode will not be used. The rounding behavior of ROUND corresponds to a value of ROUND_HALF_UP. If a different rounding behavior is wanted, use the QUANTIZE function.
If numeric-expression2 is not negative, numeric-expression1 is rounded to the absolute value of numeric-expression2 number of places to the right of the decimal point.
If numeric-expression2 is negative, numeric-expression1 is rounded to the absolute value of numeric-expression2+1 number of places to the left of the decimal point.
If the absolute value of a negative numeric-expression2 is larger than the number of digits to the left of the decimal point, the result is 0. For example, ROUND(748.58,-4) = 0. If numeric-expression1 is positive, a digit value of 5 is rounded to the next higher positive number. If numeric-expression1 is negative, a digit value of 5 is rounded to the next lower negative number.
The data type and length attribute of the result are the same as the data type and length attribute of the first argument, except that the precision is increased by one if the numeric-expression1 is DECIMAL and the precision is less than 31. For example, an argument with a data type of DECIMAL(5,2) results in DECIMAL(6,2). An argument with a data type of DECIMAL(31,2) results in DECIMAL(31,2). The scale is the same as the scale of the first argument.
If either argument can be null or if the argument is not a decimal floating-point number and the database is configured with dft_sqlmathwarn set to YES, the result can be null. If either argument is null, the result is the null value.
This function is not affected by the setting of the CURRENT DECFLOAT ROUNDING MODE special register, even for decimal floating-point arguments. The rounding behavior of ROUND corresponds to a value of ROUND_HALF_UP. If you want behavior for a decimal floating-point value that conforms to the rounding mode specified by the CURRENT DECFLOAT ROUNDING MODE special register, use the QUANTIZE function instead.
Allowable values for format-string are listed in the table of format elements listed in the Table 1.
The following format elements are used to identify the rounding or truncation unit of the datetime value in the ROUND, ROUND_TIMESTAMP, TRUNCATE and TRUNC_TIMESTAMP functions.
Table 1. Format elements for ROUND, ROUND_TIMESTAMP, TRUNCATE, and TRUNC_TIMESTAMP
Rounds up to the start of the next century after the 50th year of the century (for example on 1951-01-01-00.00.00).
Not valid for TIME argument.
Result: 1901-01-01-00.00.00.000000
Result: 1801-01-01-00.00.00.000000
Rounds up on July 1st to January 1st of the next year.
Result: 1898-01-01-00.00.00.000000
Result: 1897-01-01-00.00.00.000000
Rounds up on July 1st to the first day of the next ISO year. The first day of the ISO year is defined as the Monday of the first ISO week.
Result: 1898-01-03-00.00.00.000000
Result: 1897-01-04-00.00.00.000000
Rounds up on the 16th day of the second month of the quarter.
Result: 1999-07-01-00.00.00.000000
Result: 1999-04-01-00.00.00.000000
Rounds up on the 16th day of the month.
Result: 1999-06-01-00.00.00.000000
Rounds up on the 12th hour of the 4th day of the week, with respect to the first day of the year.
Result: 2000-05-06-00.00.00.000000
Result: 2000-04-29-00.00.00.000000
Rounds up on the 12th hour of the 4th day of the week, with respect to the first day of the ISO year.
Result: 2000-05-08-00.00.00.000000
Result: 2000-05-01-00.00.00.000000
Rounds up on the 12th hour of the 4th day of the week, with respect to the first day of the month.
Result: 2000-06-22-00.00.00.000000
Result: 2000-06-15-00.00.00.000000
Rounds up on the 12th hour of the day.
Result: 2000-05-18-00.00.00.000000
Result: 2000-05-17-00.00.00.000000
Rounds up with respect to the 12th hour of the 4th day of the week. The first day of the week is based on the locale (see locale-name).
Result: 2000-05-21-00.00.00.000000
Result: 2000-05-14-00.00.00.000000
Rounds up at 30 minutes.
Result: 2000-05-17-23.00.00.000000
Rounds up at 30 seconds.
Result: 2000-05-17-23.59.00.000000
Result: 2000-05-17-23.58.00.000000
Rounds up at half a second.
Result: 2000-05-17-23.58.46.000000
Result: 2000-05-17-23.58.45.000000
Note: The format elements in Table 1 must be specified in uppercase.
If a format element that applies to a time part of a value is specified for a date argument, the date argument is returned unchanged. If a format element that is not valid for a time argument is specified for a time argument, an error is returned (SQLSTATE 22007).
VALUES ( ROUND(873.726, 2), ROUND(873.726, 1), ROUND(873.726, 0), ROUND(873.726,-1), ROUND(873.726,-2), ROUND(873.726,-3), ROUND(873.726,-4) )
1 2 3 4 5 6 7 --------- --------- --------- --------- --------- --------- --------- 873.730 873.700 874.000 870.000 900.000 1000.000 0.000
VALUES ( ROUND(3.5, 0), ROUND(3.1, 0), ROUND(-3.1, 0), ROUND(-3.5,0) )
1 2 3 4 ---- ---- ---- ---- 4.0 3.0 -3.0 -4.0
VALUES ( ROUND(DECFLOAT('3.12350'), 3))
1 ------- 3.12400
SET :RND_DATE = ROUND(DATE('2000-08-16'), 'MONTH');
SET :RND_TMSTMP = ROUND(TIMESTAMP('2000-08-14-17.30.00'), 'YEAR');