Monday, August 8, 2011

String Functions - SQL Server 2008

String functions operate on a string input value, and return a string or a numeric value. All built in string functions are deterministic, as they return the same result for a given set of values. Below we would discuss each function in detail with examples.


ASCII(character) :
This function returns ASCII values against the input character.

Example:


This function take single character as a parameter, if you provide it a string of character. It will give you the ASCII value for the first character in the string and will ignore rest of the characters in the string.

CHAR(integer) :
This functions returns a character value against a specific ASCII value.

Example:


Integer value given as a parameter range from 0-255. For any other value, it returns NULL.

LEN(string) :
This function returns number of characters in the given character string. It does not include the trailing blank spaces.

Example:


If you want to include the trailing spaces in string length, you need to use DATALENGTH() function.


RTRIM(expression) :
If you have a string and you want to get rid of the spaces from the right side of the string then you can use this function.

Here you can see that we have select a string with one space on its left and few spaces on its right.


now if we use Rtrim, these blank spaces from the right side of the resulting string would then be removed.




LTRIM(expression) :

LTRIM works exactly the same the RTRIM works however it works on the left side of the string. It removes any space from the left side of the string.


0 comments:

Post a Comment