SET NOCOUNT ON - turns off the results count from displaying.
"OFF" is the default.
Index to this Web Page
Function | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Misc Functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
isnull() | isnull(check_expression, replacement_value)
- Replaces NULL with with the specified replacement value. Returns
the same type as the check_expression. Examples:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
String Functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ascii | ASCII Returns the ASCII code value of the leftmost character of a character expression. Syntax int = ASCII ( character_expression ) Ex: select ascii('ABC') -- 65 select ascii('A') -- 65 select ascii('Z') -- 90 select ascii('a') -- 97 select ascii('z') -- 122 select ascii('0') -- 48 select ascii('9') -- 57 select ascii('_') -- 95 select ascii(' ') -- 32 Related char() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
char | Char A string function that converts an int ASCII code to a character. The argument is an integer from 0 through 255. NULL is returned if the integer expression is not in this range. Syntax char(1) = CHAR ( integer_expression ) Examples: select char(ascii('A')) -- A select char(65) -- A select char(145) -- Left Slanted single quote: ‘ select char(146) -- Right Slanted single quote: ’ Note: SQL: char(13) = carriage return, char(10) = line feed. Similar to VB's: vbCrLf Similar to JavaScripts: \n Example: Print 'Code list: ' + char(13) + char(10) + 'A = Hello 1' + char(13) + char(10) + 'B = Hello 2'Results: Results: Code list: A = Hello 1 B = Hello 2 See also: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
charindex | CHARINDEX Returns the starting position of the specified expression in a character string. 0=Not found. 1=1st Character position. Syntax int = CHARINDEX ( expression1 , expression2 [ , start_location ] ) expression1 - search for expression2 - search in. Start_location = 0 or negative = beginning. Ex: select charindex( 'World', 'Hello World Again') -- 7 select charindex( '/', 'http://michael-thomas.com/tech/mssql/index.htm',27) -- 31 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
difference | DIFFERENCE Returns the difference between the SOUNDEX values of two character expressions as an integer. The integer returned is the number of characters in the SOUNDEX values that are the same. The return value ranges from 0 through 4, with 4 indicating the SOUNDEX values are identical. Note: SOUNDEX() returns a 4 char value. DIFFERENCE() will tell you how many of those characters are different. DIFFERENCE() evaluates the difference in the similarity of 2 strings. I'm not sure how to use this function in a real applications. I welcome any comments. Syntax int = DIFFERENCE ( character_expression , character_expression ) Example: SELECT SOUNDEX('Hello') -- H400 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
left | LEFT Returns the part of a character string starting at a specified number of characters from the left. Syntax varchar = LEFT ( character_expression , integer_expression ) Example: select left('Hello World',3) -- Hel |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
len | LEN Returns the number of characters of the given string expression, excluding trailing blanks. Syntax int = LEN ( string_expression ) Example: select len('Hello World') -- 11 select len('Hello World ') -- 11 Note: excludes the trailing blanks. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
lower | LOWER Returns a character expression after converting uppercase character data to lowercase. Syntax varchar = LOWER ( character_expression ) select lower('Hello World') -- hello world |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ltrim | LTRIM Trims the leading blanks from the left side. Syntax varchar = LTRIM ( character_expression ) select '|' + ltrim(' Hello World ' ) + '|' -- |Hello World | see also: rtrim |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
nchar | NCHAR Returns the Unicode character with the given integer code, as defined by the Unicode standard. Argument should be a positive whole number from 0 - 65535 else a NULL is returned. Syntax nchar(1) = NCHAR ( integer_expression ) select nchar(65) -- A See also: select unicode('A') -- 65 select ascii('A') -- 65 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
patindex | PATINDEX -
Returns the starting position of the first occurrence of a pattern
within the expression. Returns 0 if not found. Syntax int = PATINDEX ( '%pattern%' , expression ) select patindex('%World%', 'Hello World example') -- 7 select patindex('%ple', 'Hello World example') -- 17 select patindex('Hello%', 'Hello World example') -- 1 select patindex('%Hello', 'Hello World example') -- 0 Note: No wildcard at the end. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
replace | REPLACE Replaces the occurrences of the 2nd string with value of the 3rd string where found in the 1st string. Syntax nvarchar = REPLACE ( 'string_expression1' , 'string_expression2' , 'string_expression3' ) select replace ( 'Hello World example', 'World' , 'play' ) -- Hello play example Using replace with a field type of 'ntext'. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
quotename | QUOTENAME Returns a valid Microsoft® SQL Server™ delimited identifier. If the 'quote_character' is not specified, brackets ([ ]) is the default. Valid 'quote_character' can be: single quotation mark ('), a left or right bracket ([]), or a double quotation mark ("). "NULL" is returned for non-valid 'quote_character'. Syntax nvarchar(129) = QUOTENAME ( 'character_string' [ , 'quote_character' ] ) select quotename('Hello World Example') -- [Hello World Example] select quotename('Hello [World] Example') -- [Hello [World]] Example] select quotename('Hello World Example', '''') -- 'Hello World Example' select quotename('Hello World Example', '"') -- "Hello World Example" select quotename('Michael''s world', '''') -- 'Michael''s world' select quotename('Hello World Example', '|') -- NULL |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
replicate | REPLICATE Repeats a character expression for a specified number of times. Syntax varchar = REPLICATE ( character_expression , integer_expression ) select replicate('*',5) -- ***** select replicate('-',5) -- ----- select replicate('*-',5) -- *-*-*-*-*- |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
reverse | REVERSE Returns the reverse of a character expression. Syntax varchar = REVERSE ( character_expression ) select reverse('Hello') --olleH select reverse('Hello World') -- dlroW olleH |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
right | RIGHT Returns the specified number of chars starting from the right side. Syntax varchar = RIGHT ( character_expression , integer_expression ) select right('1234567890',3) --890 select right('Hello World',3) -- rld |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rtrim | RTRIM Trims the blanks from the right side. Syntax varchar = RTRIM ( character_expression ) select '|' + rtrim('Hello ') + '|' -- |Hello| select '|' + rtrim(' Hello') + '|' -- | Hello| see also: ltrim |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
soundex | SOUNDEX Returns a four-character (SOUNDEX) code to evaluate the similarity of two strings. Syntax char = SOUNDEX ( character_expression ) select soundex('Hello') -- H400 select soundex('Mississippi') -- M221 select soundex('Jimmy') -- J500 select soundex('Jimmie') -- J500 select soundex('abcdefg') -- A123 select soundex('tuvwxyz') -- T122 select soundex('dog') -- D200 select soundex('dot') -- D300 select soundex('Test1Test') -- T230 select soundex('Test12345678Test') -- T230 -- All numbers have the same SOUNDEX value therefore 4 is returned. select soundex('1') -- 0000 select soundex('2') -- 0000 select soundex('20') -- 0000 select soundex('100') -- 0000 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
space | SPACE Returns a string of repeated spaces. Syntax char = SPACE ( integer_expression ) select '|' + space(5) + '|' -- returns: | | (has 2 spaces) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
str | STR Returns character data converted from numeric data. Syntax char = STR ( float_expression [ , length [ , decimal ] ] ) Arguments
-- Ex: No length or decimal uses the defaults: 10-length, 0-decimal |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
stuff | stuff Deletes a specified length of characters and inserts another set of characters at a specified starting point. Syntax: --Ex: Remove a character |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
substring | SUBSTRING Returns part of an expression starting at a specified position and continuing for a specified length. Syntax <see below> = SUBSTRING ( expression , start , length ) Return types: text -> varchar image -> varbinary ntext -> nvarchar Arguments start - Starting position length - length to return from the starting position. select substring('Hello World Again',7,5) -- 'World' |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
unicode | UNICODE Returns the integer value, as defined by the Unicode standard, for the first character of the input expression. Syntax int = UNICODE ( 'ncharacter_expression' ) unicode: select unicode( 'A' ) -- 65 select unicode( 'Z' ) -- 90 See also: select nchar(65) -- A select ascii('A') -- 65 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
upper | UPPER Converts lowercase to uppercase. Syntax varchar = UPPER ( character_expression ) select upper('Hello World') -- HELLO WORLD |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Date / Time Functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
2 Digit Years | 2 digit Years. >= 50 is 19xx (ex:
1950) etc... 49 <= is 20xx (ex: 2049) -- 2 digit Years. >= 50 is 19xx (ex: 1950) etc... 49 <= is 20xx (ex: 2049) select datediff ( year , '01/01/49', '01/01/50' ) -- -99 select datediff ( year , '01/01/50', '01/01/51' ) -- 1 select datediff ( year , '01/01/2049', '01/01/2050' ) -- 1 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Misc Date/Time Info |
Misc Date/Time Info
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
convert |
Converting date/time to a character. convert(<data type>, datetime, style) Examples: select convert(varchar(255),getDate())
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dateadd |
dateadd() - Returns a new datetime value (same as the date argument)
based on adding an interval to the specified date.
Syntax <date type> = DATEADD ( datepart , number, date ) Note: <date type> - based on the 'date' argument. Arguments:
Example of Arguments for Year: select DATEADD ( year , 5, '2006-01-01 08:00:00.000' ) select DATEADD ( yyyy , 5, '2006-01-01 08:00:00.000' ) select DATEADD ( yy , 5, '2006-01-01 08:00:00.000' ) -- 2 digit Years. >= 50 is 19xx (ex: 1950) etc... 49 <= is 20xx (ex: 2049) select DATEADD ( year , 10, '01/01/49' ) -- 2059-01-01 00:00:00.000 select DATEADD ( year , 10, '01/01/50' ) -- 1960-01-01 00:00:00.000 Seen as 1950 and not 2050! select DATEADD ( year , 10, '01/01/2050' ) -- 2060-01-01 00:00:00.000 --Example of the different date parts. select DATEADD ( yy , 1, '2006-01-01 08:00:00.000' ) -- 2007-01-01 08:00:00.000 select DATEADD ( qq , 1, '2006-01-01 08:00:00.000' ) -- 2006-04-01 08:00:00.000 select DATEADD ( mm , 1, '2006-01-01 08:00:00.000' ) -- 2006-02-01 08:00:00.000 select DATEADD ( dy , 1, '2006-01-01 08:00:00.000' ) -- 2006-02-01 08:00:00.000 select DATEADD ( dd , 1, '2006-01-01 08:00:00.000' ) -- 2006-02-01 08:00:00.000 select DATEADD ( ww , 1, '2006-01-01 08:00:00.000' ) -- 2006-01-08 08:00:00.000 select DATEADD ( hh , 1, '2006-01-01 08:00:00.000' ) -- 2006-01-01 09:00:00.000 select DATEADD ( mi , 1, '2006-01-01 08:00:00.000' ) -- 2006-01-01 08:01:00.000 select DATEADD ( ss , 1, '2006-01-01 08:00:00.000' ) -- 2006-01-01 08:00:01.000 select DATEADD ( ms , 500, '2006-01-01 08:00:00.000' ) -- 2006-01-01 08:00:00.500 -- Example: Today's date (Add, Sub) select getdate() as 'now', dateadd(dd,-1,getdate()) as 'Subtract 1 day', dateadd(dd,1,getdate()) as 'Add 1 day' -- 2007-10-18 10:42:25.513 2007-10-17 10:42:25.513 2007-10-19 10:42:25.513 select getdate() 'now', dateadd(hh,-1,getdate()) as 'Sub 1 hour', dateadd(hh,1,getdate()) as 'Add 1 hour' --2008-01-02 13:00:45.967 2008-01-02 12:00:45.967 2008-01-02 14:00:45.967 (Note: for UTC time use: getutcdate() ) -- Warning: Milliseconds seems to have issues: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
datediff | datediff - returns the difference of 2 dates based on the "datepart"
argument. -- yy or yyyy - Returns the difference of year units based on the Year values (doesn't look at the Month or Day) -- q or qq - Returns the difference of quarter units based on the Month & Year values (doesn't look at the Day). -- m or mm - Returns the difference of month units based on the Month & Year (doesn't look at the day) Syntax int = DATEDIFF ( datepart , startdate , enddate ) Max for milliseconds: 24 days, 20 hours, 31 minutes and 23.647 seconds. Max for seconds: 68 years. Arguments:
Example of Arguments for Year: select datediff(year, '12/31/2000', '01/01/2001') -- 1 - only 1 day, but returns 1. select datediff(yy, '12/31/2000', '01/01/2001') -- 1 - only 1 day, but returns 1. select datediff(yyyy, '12/31/2000', '01/01/2001') -- 1 - only 1 day, but returns 1. -- 2 digit Years. >= 50 is 19xx (ex:
1950) etc... 49 <= is 20xx (ex: 2049) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
datename | DATENAME - Returns a character string
representing the specified datepart of the specified date. (see DATEPART for int) Syntax nvarchar = DATENAME ( datepart , date ) Arguments:
select DATENAME ( yy , '2006-12-31 12:59:59.001' ) -- 2006 select DATENAME ( qq , '2006-12-31 12:59:59.001' ) -- 4 select DATENAME ( mm , '2006-12-31 12:59:59.001' ) -- December (see datepart for a int) select DATENAME ( dy , '2006-12-31 12:59:59.001' ) -- 365 select DATENAME ( dd , '2006-12-31 12:59:59.001' ) -- 31 select DATENAME ( ww , '2006-12-31 12:59:59.001' ) -- 53 select DATENAME ( hh , '2006-12-31 08:00:00.001' ) -- 8 (24hr clock!) select DATENAME ( hh , '2006-12-31 12:00:00.001' ) -- 12 (24hr clock!) select DATENAME ( hh , '2006-12-31 17:00:00.001' ) -- 17 (24hr clock!) select DATENAME ( mi , '2006-12-31 12:59:59.001' ) -- 59 select DATENAME ( ss , '2006-12-31 12:59:59.001' ) -- 59 select DATENAME ( ms , '2006-12-31 12:59:59.001' ) -- 0 (Warning: Expected '001') |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
datepart | DATEPART - Returns an integer representing
the specified datepart of the specified date. (see DATENAME for string) Syntax
select DATEPART ( yy , '2006-12-31 12:59:59.001' ) -- 2006 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
day | DAY - Returns an integer representing the
day of the specified date. Syntax DAY ( date ) (Note: equivalent to DATEPART(dd, date)) select DAY ( '2006-12-31 12:59:59.001' ) -- 31 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
getdate | GETDATE Returns the current system date and time in the Microsoft® SQL Server™ standard internal format for datetime values. Syntax datetime = GETDATE ( ) (Note: equivalent to DATEPART(mm, date).) select GETDATE ( ) -- 2006-04-24 13:01:12.137 see also: getutcdate() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
month | MONTH - Returns an integer that represents
the month part of a specified date. Syntax int = MONTH ( date ) select MONTH ( '2006-12-31 12:59:59.001' ) -- 12 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
year | YEAR - Returns an integer that represents
the year part of a specified date. Syntax int = YEAR ( date ) (Note: equivalent to DATEPART(yy, date).) select YEAR ( '2006-12-31
12:59:59.001' ) -- 2006 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
getutcdate | GETUTCDATE - Returns the datetime value
representing the current UTC time (Universal Time Coordinate or
Greenwich Mean Time). The current UTC time is derived from the current
local time and the time zone setting in the operating system of the
computer on which SQL Server is running. Syntax datetime = GETUTCDATE() Remarks - from "MSSQL Transact SQL Reference" GETUTCDATE is a nondeterministic function. Views and expressions that reference this column cannot be indexed. GETUTCDATE cannot be called inside a user-defined function. My notes: Very handy because of time zones. select GETUTCDATE() -- 2006-04-24 17:13:03.513 ( 5:13pm GMT ) select GETDATE() -- 2006-04-24 13:13:03.513 ( 1:13pm EST ) Time Zones: select getdate() – date/time of the server’s timezone. select getutcdate() – GMT select dateadd(hh,-5,getutcdate()) – if server is in the EST, then this is the same as getdate() select dateadd(hh,-4,getutcdate()) – if server is in the EDT, then this is the same as getdate() see also: getdate() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Math Functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
abs | ABS Returns the absolute, positive value of the given numeric expression. Syntax <Same type> = ABS ( numeric_expression ) select abs(-1) -- 1 select abs(-1.0) -- 1.0 select abs (-1.5) -- 1.5 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
acos | ACOS Returns the angle, in radians, whose cosine is the given float expression; also called arccosine. Syntax float = ACOS ( float_expression ) Arguments float_expression - float or real with a value from -1 through 1. select acos(-1) -- 3.1415926535897931 select acos(1) -- 0.0 select acos(.015) -- 1.5557957642379359 select acos(-1.5) -- A domain error occurred. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
asin | ASIN - Returns the angle, in radians,
whose sine is the given float expression (also called arcsine). Syntax float = ASIN ( float_expression ) Argument: Values from -1 through 1 select asin(1.00) -- 1.5707963267948966 select asin(-1.00)-- 1.5707963267948966 select asin(0.99) -- 1.4292568534704693 select asin(-0.99) -- -1.4292568534704693 select asin(1.01) -- Messages: A domain error occurred. select asin(-1.01) -- Messages: A domain error occurred. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
atan | ATAN - Returns the angle in radians whose
tangent is the given float expression (also called arctangent). Syntax float = ATAN ( float_expression ) select atan(360) -- 1.568018556161576 select atan(180) -- 1.568018556161576 select atan(90) -- 1.5652408283942041 select atan(45) -- 1.5596856728972892 select atan(0) -- 0.0 select atan(-1) -- -0.78539816339744828 select atan(900) -- 1.568018556161576 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
atn2 | ATN2 - Returns the angle, in radians,
whose tangent is between the two given float expressions (also called
arctangent). Syntax float = ATN2 ( float_expression , float_expression ) select atn2(360, 180) -- 1.1071487177940904 select atn2(180, 90) -- 1.1071487177940904 select atn2(360, 0) -- 1.5707963267948966 select atn2(360, 90) -- 1.3258176636680326 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ceiling | CEILING - Returns the smallest integer
greater than, or equal to, the given numeric expression. Syntax <same as argument> = CEILING ( numeric_expression ) See also: floor select ceiling ( 100.00 ) -- 100 select ceiling ( 100.01 ) -- 101 select ceiling ( 100.50 ) -- 101 select ceiling ( 100.40 ) -- 101 select ceiling ( -1 ) -- -1 select ceiling ( -1.01 ) -- -1 select ceiling ( -1.50 ) -- -1 select ceiling ( -1.99 ) -- -1 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cos | COS - A mathematic function that returns
the trigonometric cosine of the given angle (in radians) in the given
expression. Syntax float = COS ( float_expression ) select cos(360) -- -0.28369109148652732 select cos(180) -- -0.59846006905785809 select cos(90) -- -0.44807361612917013 select cos(45) -- 0.52532198881772973 select cos(0) -- 1.0 select cos(-1) -- 0.54030230586813977 select cos(900) -- 0.06624670220315812 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cot | COT - A mathematic function that returns
the trigonometric cotangent of the specified angle (in radians) in the
given float expression. Syntax float = COT ( float_expression ) select cot(360) -- -0.29584569796855498 select cot(180) -- 0.74699881441404437 select cot(90) -- -0.50120278338015323 select cot(45) -- 0.61736962378355509 select cot(0) -- 1.0 -- A domain error occurred. select cot(-1) -- -0.64209261593433076 select cot(900) -- 6.6392548412446309E-2 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
degrees | DEGREES - Returns the corresponding angle
in degrees for a given an angle in radians, Syntax <same as argument> = DEGREES ( numeric_expression ) select degrees(radians(45)) -- 0 select degrees(radians(-45)) -- 0 select degrees(radians(90)) -- 57 select degrees(radians(-90)) -- -57 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
exp | EXP - Returns the exponential value of the
given float expression. Syntax float = EXP ( float_expression ) select exp ( 123.123456789 ) -- 2.9637248139167939E+53 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
floor | FLOOR - Returns the largest integer less
than or equal to the given numeric expression. Syntax <same as argument> = FLOOR ( numeric_expression ) select floor ( 100.00 ) -- 100 select floor ( 100.01 ) -- 100 select floor ( 100.50 ) -- 100 select floor ( 100.99 ) -- 100 select floor ( -1 ) -- -1 select floor ( -1.01 ) -- -2 select floor ( -1.50 ) -- -2 select floor ( -1.99 ) -- -2 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
log | LOG - Returns the natural logarithm of the
given float expression. Syntax float = LOG ( float_expression ) select LOG ( 1.00 ) -- 0 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
log10 | LOG10 - Returns the base-10 logarithm of
the given float expression. Syntax float = LOG10 ( float_expression ) select LOG10 ( 1.00 ) -- 0 select LOG10 ( 1.5 ) -- 0.17609125905568124 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pi | PI - Returns the constant value of PI. Syntax float = PI ( ) select pi() -- 3.1415926535897931 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
power | POWER Returns the value of the given expression to the specified power. Syntax <same as 1st arg> = POWER ( numeric_expression , y ) y - Is the power to which to raise numeric_expression. y can be an expression of the exact numeric or approximate numeric data type category, except for the bit data type. select power ( 100 , 1 ) -- 100 select power ( 100 , 2 ) -- 10000 select power ( 100 , 3 ) -- 1000000 select power ( 3 , 1 ) -- 3 select power ( 3 , 2 ) -- 9 select power ( 3 , 3 ) -- 27 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
radians | RADIANS - Returns radians when a numeric
expression, in degrees, is entered. Syntax <numeric_expression> = RADIANS ( numeric_expression ) select radians(45) -- 0 select radians(-45) -- 0 select radians(90) -- 1 select radians(-90) -- -1 select radians(180) -- 3 select radians(-180) -- -3 select radians(360) -- 6 select radians(-360) -- -6 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rand | RAND - Returns a random float value from 0
through 1. Warning: I don't like the way this function works. I find it hard to return a random # from 1-10 (or 1-100 etc...) with this function. Syntax float = RAND ( [ seed ] ) seed - integer expression like: int, tinyint, smallint SET NOCOUNT ON --Look at the Messages Tab select RAND ( 1 ) -- 0.71359199321292355 select RAND ( 1 ) -- 0.71359199321292355 select RAND ( -1 ) -- 0.71359199321292355 select RAND ( 1000 ) -- 0.73220633149986536 select RAND ( -1000 ) -- 0.73220633149986536 print RAND ( 1 ) -- 0.713592 (Value is truncated) print RAND ( 1 ) -- 0.713592 (Value is truncated) DECLARE @count int DECLARE @seed int DECLARE @multiplier int SET @count = 1 SET @multiplier = 100 WHILE @count < 10 BEGIN set @seed = @count * @multiplier print 'Count=' + convert(nvarchar, @seed) + ', Rand=' + convert(nvarchar, RAND(@seed * 100)) SET @count = @count + 1 END SET NOCOUNT OFF GO /* Results: (Will be the same everytime it is run.) Count=100, Rand=0.899903 Count=200, Rand=0.0862328 Count=300, Rand=0.272563 Count=400, Rand=0.458892 Count=500, Rand=0.645222 Count=600, Rand=0.831552 Count=700, Rand=0.0178814 Count=800, Rand=0.204211 Count=900, Rand=0.390541 Note: If the multiplier is 1 then the
following results: */
Random #s from 1 to x:
Example of the DateTime Warning: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
round | ROUND - Returns a numeric expression,
rounded to the specified length or precision. Syntax <same as arg> = ROUND ( numeric_expression , length [ , function ] ) function - If 0 normal rounding. If not 0, then truncate #'s past the length and then round. select round ( 100.4 , 0 ) -- 100.0 select round ( 100.5 , 0 ) -- 101.0 select round ( 100.49 , 0 ) -- 100.0 select round ( 100.49 , 1 ) -- 100.50 select round ( 100.449 , 1 ) -- 100.400 -- Example of the "function" to truncate, then round. select round ( 100.49 , 1, 0 ) -- 100.50 select round ( 100.49 , 1, 1 ) -- 100.40 (.4 vs .49 - A value other than 0 will truncate values past the round #. ) select round ( 100.12345 , 0 ) -- 100.00000 select round ( 100.12345 , 1 ) -- 100.10000 select round ( 100.12345 , 2 ) -- 100.12000 select round ( 100.12345 , 3 ) -- 100.12300 Here is some weird ones: select convert(decimal,140813)/1000/60 -- returns 2.346883333 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sign | SIGN - Returns the positive (+1), zero
(0), or negative (-1) sign of the given expression. Syntax float = SIGN ( numeric_expression ) select sign ( 0 ) -- 0 select sign ( 1 ) -- 1 select sign ( -1 ) -- -1 select sign ( 100 ) -- 1 select sign ( -100 ) -- -1 select sign ( 55 ) -- 1 select sign ( -55 ) -- -1 select sign ( 2 ) -- 1 select sign ( -2 ) -- -1 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sin | SIN - Returns the trigonometric sine of
the given angle (in radians) in an approximate numeric (float)
expression. Syntax float = SIN ( float_expression ) select SIN ( 45 ) -- 0.85090352453411844 select SIN ( 90 ) -- 0.89399666360055785 select SIN ( 180 ) -- -0.80115263573383044 select SIN ( 360 ) -- 0.95891572341430653 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
square | SQUARE - Returns the square of the given
expression. Syntax float = SQUARE ( float_expression ) select SQUARE ( 3 ) -- 9.0 select SQUARE ( 9 ) -- 81.0 select SQUARE ( 12 ) -- 144.0 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sqrt | SQRT - Returns the square root of the
given expression. Syntax float = SQRT ( float_expression ) select sqrt ( 9 ) -- 3.0 select sqrt ( 81 ) -- 9.0 select sqrt ( 144 ) -- 12.0 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
tan | TAN - Returns the tangent of the input
expression. Syntax float = TAN ( float_expression ) select tan(pi()/2) -- 1.6331778728383844E+16 |