Tag: STR() function in SQL Server

STR() function in SQL Server

Today, we are going to see the function – STR in SQL Server.

STR ( float_expression [ , length [ , decimal ] ] )
Microsoft defines the function as “Returns character data converted from numeric data. The character data is right-justified, with a specified length and decimal precision.” The key words are “right -justified” and “specified length and decimal precision”. Let us go through an example to understand these two terms.
Select '*'+ str(1) + '*' Value,Len(str(1)) Value_Len
The output is clear that the value “1” is right justified to 10 positions by default.

Few more interesting observations:
    1. By default, if the value is more than 10, then the value of the result is masked to “*” to 10 times.
    2. If the length is specified, and the value is more than specified length, then again it masks to specified length.

Round up funda:
    1. If length is not specified, then depending the decimal part, the number is rounded to next.
    2. Adds more 0 to match the decimal part, if in short the number for decimal.

I’d like to grow my readership. If you enjoyed this blog post, please share it with your friends!