STR Function in SQL Server

STR is a specialized conversion function that always converts from a number (for example, float or numeric) to a character data type. It allows you to explicitly specify the length of the string and the number of decimal places that should be formatted. The general form of the syntax for the STR function is:

STR(float expression, character length, number of decimal places)

character length” must include room for a decimal place and a negative sign. STR rounds a value to the number of decimal places requested. We will illustrate the use of the STR function using the Employee table. In this table, the wage column is a SMALLMONEY column, so it.

Displays with two decimal places. To format it to one decimal place, we can use STR. Note that we made the character length 4 in this case in order to accommodate the .0 (the decimal point and zero). The following query shows this:

SELECT names, wage = STR(wage, 4, 1), hours
FROM Employee

This produces:

Names Wage Hours
Sumon Bagui 10.0 40
Sudip Bagui 15.0 30
Priyashi Saha 18.0 NULL
Ed Evans NULL 10