Share via

Apply SQL transformation : conditional rounding

Mohamed El Hamrouni 231 Reputation points
2021-01-25T07:47:23.52+00:00

Dear,

I would like for a specidified column ("PagesTot"), apply a rounding (to 100's) when the number of pages goes over 500. I use for this the "apply sql transformation" module.
SELECT PagesTot,
CASE
WHEN [PagesTot]>500 THEN round(PagesTot,100)
ELSE PagesTot
END AS PagesTot
FROM t1;

** What am I doing wrong as the rounding does not apply and I would like to keep the column name and the module is creating a new column name ("PagesTot(2)").

Thank for your hep.

Mohamed.

Azure SQL Database
0 comments No comments

Answer accepted by question author

Anurag Sharma 17,636 Reputation points
2021-01-25T09:38:31.23+00:00

Hi @Mohamed El Hamrouni , welcome to Microsoft Q& forum.

Above statement is creating PagesTot(2) because you have used PagesTot twice in your select statement.

To apply the rounding correctly, you can try below query:

SELECT PagesTot,CASE  
WHEN [PagesTot]>500 THEN round(PagesTot,-2)  
ELSE PagesTot  
END AS PagesTotNew  
FROM t1;  

Please note '-2' it is for 100. If we want 1000 we need to use '-3'. Refer to link for more details.

This query would round the number to closest 100. That means:

601-649 would return 600
650-699 would return 700

Please let me know if this helps or else we can discuss further on the same.

----------

If answer helps, please select 'Accept answer' as it could help other having similar queries.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.