How convert numeric datatype to string by STR function?

For convert numeric datatype to string T-SQL provides `STR`

function.

This function get one required parameter: float expression.Seconad is optional parameter length of chars this number, default is 10. And third parameter is the number of places after decimal point char, default is 16.

The function returns varchar datatype.

It is examples using `STR`

function.

SELECT '='+STR(12.8)+'=' as stringdata ;

Default number of length string is 10, so You may see added 8 spaces in front of string.

SELECT '='+STR(12.8, 5)+'=' as stringdata ;

The number of max length return string is 5 given in parameter, so You may see added 3 spaces in front of string.

SELECT '='+STR(12.8, 2)+'=' as stringdata ;

The number of max length return string is 2 given in parameter, The float number has 4 sign so so 2 last chars was deleted.

SELECT '='+STR(12.8, 1)+'=' as stringdata ;

The number of max length return string is 1 given in parameter, The float number has 4 sign, but 2 are as main number places before point so the number may not be converted. Therefore in string place is insert * sign.

SELECT '='+STR(12.8,4,2)+'=' as stringdata ;

The number of max length return string is 4 given in parameter, The float number has 4 sign so will be all chars display. The number of places after decimal point char is in last parameter as 2, but the max all chars may be 4 so zero not be added.

SELECT '='+STR(12.8,5,2)+'=' as stringdata ;

In this situation the number of max length string is 5 given in parameter, The float number has 4 sign so will be all chars display and 1 sign is free. The number of places after decimal point char is in last parameter as 2, but the max all chars may be 4 so zero is added as fifth char.

SELECT '='+STR(12.8,8,2)+'=' as stringdata ;

In this situation the number of max length string is 8 given in parameter, The float number has 4 sign so will be all chars display and 4 sign is free. The number of places after decimal point char is in last parameter as 2, but the max all chars may be 4 so zero is added as fifth char. Befor string is added spaces – 3 chars.

SELECT '='+STR(12.8734223344566,20,13)+'=' as stringdata ;