Thursday, February 15, 2024

Rounding Numbers to Nearest Integer

 In one of my project, we are preparing tech refresh for the system and we need to set the ID numbers for all the tables with higher values in the setup table. The values of ID numbers are vary and need to set full integer for example the last ID number is 35,577,991, we would like to set as 30,000,000 or 40,000,000. It is easy to achieve in T-sql using the Round function as follow:

SELECT ROUND(35577991, (LEN(35577991)-1)*-1)

SQL Server will give you 40,000,000 as the value is greater than 35,000,000. If the value is less than 35,000,000, the result will be 30,000,000.

No comments: