Tải bản đầy đủ (.pdf) (9 trang)

Tài liệu Using Functions phần 2 doc

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (47.58 KB, 9 trang )

You use the POWER() function to get the value of a number raised to a specified power.
The following example returns 8:
SELECT POWER(2, 3);
You use the ROUND() function to get the value of a number rounded or truncated to a
specified length. The following example returns 1.23500, which is 1.23456 rounded to
three decimal places:
SELECT ROUND(1.23456, 3);
The next example passes a non-zero number as the third parameter to ROUND(), which
indicates that the number is to be truncated rather than rounded, as was done in the
previous example:
SELECT ROUND(1.23456, 3, 1);
This example returns 1.23400, which is 1.23456 truncated to three decimal places.
You use the SQUARE() function to get the square of a number. The following example
returns 16.0:
SELECT SQUARE(4);
You use the SQRT() function to get the square root of a number. The following example
returns 4.0:
SELECT SQRT(16);
Using String Functions
The string functions allow you to manipulate strings. For example, you can replace
specified characters in a string. Table 4.5
lists the string functions available in SQL
Server.
Table 4.5: STRING FUNCTIONS
FUNCTION DESCRIPTION
ASCII(charExpression) Returns the ASCII code for the leftmost character of
charExpression.
CHAR(intExpression) Returns the character that corresponds to the ASCII
code specified by intExpression.
CHARINDEX (charExpression1, Returns the position of the characters specified by
Table 4.5: STRING FUNCTIONS


FUNCTION DESCRIPTION
charExpression2 [, start ]) charExpression1 in charExpression2, starting at the
optional position specified by start.
DIFFERENCE (charExpression1,
charExpression2)
Returns the difference between the SOUNDEX
values of the two character expressions. You use the
SOUNDEX code to evaluate the phonetic similarity
of two strings. The returned value is between 0 and
4; 4 indicates that the two expressions are
phonetically identical.
LEFT(charExpression,
intExpression)
Returns the leftmost characters specified by
intExprssion from charExpression.
LEN(charExpression) Returns the number of characters in charExpression.
LOWER(charExpression) Converts the characters in charExpression to
lowercase and returns those characters.
LTRIM(charExpression) Removes any leading spaces from the start of
charExpression and returns the remaining characters.
NCHAR(intExpression) Returns the Unicode character with the code
specified by intExpression.
PATINDEX('%pattern%',
charExpression)
Returns the starting position of the first occurrence
of pattern in charExpression. If pattern is not found
then zeros are returned.
REPLACE (charExpression1,
charExpression2,
charExpression3)

Replaces all occurrences of charExpression2 in
charExpression1 with charExpression3.
QUOTENAME ('charString' [ ,
'quoteChar' ])
Returns a Unicode string with the delimiters
specified by quoteChar added to make charString a
valid delimited identifier.
REPLICATE (charExpression,
intExpression)
Repeats charExpression a total of intExpression
times.
REVERSE(charExpression) Reverses the characters in charExpression and
returns those characters.
RIGHT(charExpression,
intExpression)
Returns the rightmost characters specified by
intExprssion from charExpression.
RTRIM(charExpression) Removes any trailing spaces from the end of
charExpression and returns the remaining characters.
SOUNDEX(charExpression) Returns the four-character SOUNDEX code. You
use this code to evaluate the phonetic similarity of
Table 4.5: STRING FUNCTIONS
FUNCTION DESCRIPTION
two strings.
SPACE(intExpression) Returns a string of repeated spaces for a total
specified by intExpression.
STR(floatExpression [ , length [ ,
decimal ] ])
Converts the number specified by floatExpression to
characters; length specifies the total number of

characters you want to see (including digits and
spaces, plus the positive or negative sign and
decimal point); decimal specifies the number of
digits to the right of the decimal point. The number
is rounded if necessary.
STUFF (charExpression1, start,
length, charExpression2)
Deletes characters from charExpression1, starting at
the position specified by start for a total of length
characters, and then inserts the characters specified
by charExpression2.
SUBSTRING(expression, start,
length)
Returns part of a character, binary, text, or image
expression.
UNICODE('nCharExpression') Returns the Unicode value for the first character of
the nchar or nvarchar expression nCharExpression.
UPPER(charExpression) Converts the characters in charExpression to
uppercase and returns those characters.
Let's consider examples that use some of the string functions.
You use the ASCII() function to get the ASCII code for the leftmost character of the
supplied character expression. The following example returns 65 and 97:
SELECT ASCII('A'), ASCII('a');

You use the CHAR() function to get the character that corresponds to the ASCII code of
the supplied integer expression. The following example returns A and a:
SELECT CHAR(65), CHAR(97);
You use the CHARINDEX() function to get the position of characters. The following
example returns 16, which is the position where the word ten starts:
SELECT CHARINDEX('ten', 'Four-score and ten years');

You use the DIFFERENCE() function to obtain the difference between the SOUNDEX
values of two character expressions. The following example returns 4, indicating that
Brown and Browne are phonetically identical:
SELECT DIFFERENCE('Brown', 'Browne');
You use the LEFT() function to obtain the leftmost characters of a character expression.
The following example returns Four-score, which are the 10 leftmost characters of Four-
score and ten years:
SELECT LEFT('Four-score and ten years', 10);
You use the RIGHT() function to obtain the rightmost characters of a character
expression. The following example returns years, which are the five rightmost characters
of Four-score and ten years:
SELECT RIGHT('Four-score and ten years', 5);
You use the LEN() function to obtain the digits in a character expression. The following
example returns 24:
SELECT LEN('Four-score and ten years');
You use the LOWER() function to obtain the lowercase version of a character expression.
The following example returns four-score and ten years:
SELECT LOWER('FOUR-SCORE AND TEN YEARS');
You use the UPPER() function to obtain the uppercase version of a character expression.
The following example returns FOUR-SCORE AND TEN YEARS:
SELECT UPPER('four-score and ten years');
You use the LTRIM() and RTRIM() functions to remove any spaces from the left and
right of a character expression. The following example returns FOUR-SCORE and AND
TEN YEARS (spaces removed):
SELECT LTRIM(' FOUR-SCORE'), RTRIM('AND TEN YEARS ');
You use the STR() function to convert a numeric value to a string consisting of numbers.
The first parameter is the number to convert, the second is the total number of characters
you want in your string, and the third is the number of digits after the decimal point. The
following example returns 123.46:
SELECT STR(123.456, 6, 2);

The number 123.456 is converted to a string of six characters, with two digits after the
decimal point, and rounded.
You use the STUFF() function to replace characters. The first parameter is the string you
want to replace characters in, the second is the starting position, the third is the total
number of characters, and the fourth is the set of characters to insert. The following
example returns Five-score and ten:
SELECT STUFF('Four-score and ten', 1, 4, 'Five');
Four is replaced with Five.
You use the SUBSTRING() function to obtain part of a string. The first parameter is the
string, the second is the starting position, and the third is the total number of characters.
The following example returns Four:
SELECT SUBSTRING('Four-score and ten', 1, 4);
You use the UNICODE() function to obtain the Unicode value for the first character. The
following example returns 65 and 97:
SELECT UNICODE('A'), UNICODE('a');
Using Date and Time Functions
The date and time functions allow you to manipulate dates and times. For example, you
can add a number of days to a given date. Table 4.6
lists the date and time functions
available in SQL Server.
Table 4.6: DATE AND TIME FUNCTIONS
FUNCTION DESCRIPTION
DATEADD(interval,
number, date)
Returns a datetime that is the result of adding the specified
number of interval units to date. Valid intervals include
year, quarter, month, dayofyear, day, week, hour, minute,
second, and millisecond.
DATEDIFF (interval,
startDate, endDate)

Returns the difference between startDate and endDate, with
the difference calculated in interval units (year, quarter, and
so on).
DATENAME(interval,
date)
Returns a character string that represents the name of
interval part of date.

×