Microsoft® SQL Server® 2012 Bible

(Ben Green) #1

199


Chapter 8: Data Types, Expressions, and Scalar Functions


8


RIGHT (string, count) and LEFT (string, count)
RIGHT and LEFT Returns the rightmost or leftmost part of a string:

SELECT LEFT('LeBlanc',2) AS [Left] ,
RIGHT('LeBlanc',2) AS [Right];

Result:

Left Right
----- ----
Le nc

LEN (string)
LEN returns the length of a string:

SELECT LEN('Supercalifragilisticexpialidocious') AS [Len];

Result:

Len
-----------
34

RTRIM(string) and LTRIM(string)
RTRIM(string) and LTrim(string)remove leading or trailing spaces. Although it’s dif-
fi cult to see in print, the three leading and trailing spaces are removed from the following
string. They are often used together as RTRIM(LTRIM(string). The column-header lines
are adjusted with the remaining spaces to illustrate the functions:

SELECT RTRIM(' middle earth ') AS [RTrim],
LTRIM(' middle earth ') AS [LTrim];

Result:

RTrim LTrim
--------------- ---------------
middle earth middle earth

UPPER(string) and LOWER(string):
UPPER and LOWER convert the entire string to uppercase or lowercase. There’s not much to
know about these two functions:

SELECT UPPER('one TWO tHrEe') AS UpperCase,
LOWER('one TWO tHrEe') AS LowerCase;

Result:

UpperCase LowerCase
------------- -------------
ONE TWO THREE one two three

c08.indd 199c08.indd 199 7/30/2012 4:21:16 PM7/30/2012 4:21:16 PM


http://www.it-ebooks.info
Free download pdf