I have a need to format numbers with padded zeros left and right. The number's precision is decimal (9,5). I am converting to a character string and need it to be a length of 4. I am using SQL server 2008.
Here are some examples:
If 3.0 then I need 0003
If 30.0 then I need 0030
If 112.8 then I need 1128
If 120.0 then I need 1200
and so on.
I have tried multiple format functions like Right, Left, Replace, etc. But no combinations seem to get me the correct format.
Example below:
Right('00' + Replace(SUBSTRING(CAST(My_Table.My_Field as varchar),1,4),'.',''),4)
It works fine for the number 36.0 but this gets me 0212 for the number 212.0. I need it to be 2120