STR (Transact-SQL)
Returns character data converted from numeric data.
Transact-SQL Syntax Conventions
Syntax
STR ( float_expression [ , length [ , ] ] )
Arguments
float_expression
Is an expression of approximate numeric (float) data type with a decimal point.
length
Is the total length. This includes decimal point, sign, digits, and spaces. The default is 10.
decimal
Is the number of places to the right of the decimal point. decimal must be less than or equal to 16. If decimal is more than 16 then the result is truncated to sixteen places to the right of the decimal point.
Return Types
char
Remarks
If supplied, the values for length and decimal parameters to STR should be positive. The number is rounded to an integer by default or if the decimal parameter is 0. The specified length should be greater than or equal to the part of the number before the decimal point plus the number's sign (if any). A short float_expression is right-justified in the specified length, and a long float_expression is truncated to the specified number of decimal places. For example, STR(12,10) yields the result of 12. This is right-justified in the result set. However, STR(1223,2) truncates the result set to **. String functions can be nested.
Note:
To convert to Unicode data, use STR inside a CONVERT or CAST conversion function.
Examples
The following example converts an expression that is made up of five digits and a decimal point to a six-position character string. The fractional part of the number is rounded to one decimal place.
Copy Code
SELECT STR(123.45, 6, 1)
GO
Here is the result set.
SQL Server 2005 Books Online
ISNULL (Transact-SQL)
Replaces NULL with the specified replacement value.
Transact-SQL Syntax Conventions
Syntax
ISNULL ( check_expression , replacement_value )
Arguments
check_expression
Is the expression to be checked for NULL. check_expression can be of any type.
replacement_value
Is the expression to be returned if check_expression is NULL. replacement_value must be of a type that is implicitly convertible to the type of check_expresssion.
Return Types
Returns the same type as check_expression.
Remarks
The value of check_expression is returned if it is not NULL; otherwise, replacement_value is returned after it is implicitly converted to the type of check_expression, if the types are different.
Examples
A. Using ISNULL with AVG
The following example finds the average of the weight of all products. It substitutes the value 50 for all NULL entries in the Weight column of the Product table.
Copy Code
USE AdventureWorks;
GO
SELECT AVG(ISNULL(Weight, 50))
FROM Production.Product;
GO
Here is the result set.
Copy Code
--------------------------
59.79
(1 row(s) affected)
B. Using ISNULL
The following example selects the description, discount percentage, minimum quantity, and maximum quantity for all special offers in AdventureWorks. If the maximum quantity for a particular special offer is NULL, the MaxQty shown in the result set is 0.00.
Copy Code
USE AdventureWorks;
GO
SELECT Description, DiscountPct, MinQty, ISNULL(MaxQty, 0.00) AS 'Max Quantity'
FROM Sales.SpecialOffer;
GO
Returns a character expression after it removes leading blanks.
Transact-SQL Syntax Conventions
Syntax
LTRIM ( character_expression )
Arguments
character_expression
Is an expression of character or binary data. character_expression can be a constant, variable, or column. character_expression must be of a data type, except text, ntext, and image, that is implicitly convertible to varchar. Otherwise, use CAST to explicitly convert character_expression.
Return Type
varchar or nvarchar
Remarks
Compatibility levels can affect return values. For more information about compatibility levels, see sp_dbcmptlevel (Transact-SQL).
Examples
The following example uses LTRIM to remove leading spaces from a character variable.
Copy Code
DECLARE @string_to_trim varchar(60)
SET @string_to_trim = ' Five spaces are at the beginning of this
string.'
SELECT 'Here is the string without the leading spaces: ' +
LTRIM(@string_to_trim)
GO
Here is the result set.
Copy Code
------------------------------------------------------------------------
Here is the string without the leading spaces: Five spaces are at the beginning of this string.