5. Function introduction

String functions

ASCII ( character expression )    ASCII function, returns the ASCII code value of the leftmost character in the string expression
CHAR ( integer expression )    converts the ASCII code value to the corresponding character. The parameter is an integer between 0 and 255 . If no ASCII code value between 0 and 255 is entered , CHAR () returns NULL .
LEN ( character expression )    string length function, returns the number of characters in the string expression
LOWER ( character expression ) lowercase letter function, convert uppercase letters to lowercase letters, return the lowercase letters of the expression
UPPER ( character expression ) uppercase letter function, returns the uppercase letter representation of the specified expression
The REPLACE ( character expression 1 , a character expression 2 , expression of the character of formula 3) character replacement functions, the replacement first string expression with a third expression after all expressions included in the second character, Jian return replacement Expression
LEFT ( character expression, n) left substring function, returns n characters starting from the left side of the string
RIGHT ( character expression, n) right substring function, returns n characters starting from the right side of the string
LTRIM ( character expression ) deletes the head space function, returns the character expression with leading spaces removed
RTRIM ( character expression ) delete trailing spaces function, returns the character expression that deletes all trailing spaces
The STR ( numerical expression [ , length of the n-[ , decimal ]]) numbers to character conversion function that returns a numeric string conversion over the length specifies the total length, including the decimal point, decimal number of digits to the right of the decimal point
 
-[Example] Use the commonly used string function to calculate the ASCII code of A, the first 3 characters of 'hello', the length of the 'database principle' string, convert 'hello to uppercase letters, and change' English Level 4 'to 'English Level 6'.
select N'A ASCII '= ASCII (' A '),
            the first three characters of N'hello' = LEFT ('hello', 3),
            the length of N 'database principle' = LEN (N 'database principle') ,
            N 'converts hello to uppercase letters' = UPPER ('hello'),
            N 'changes English level 4 to level 6' = Replace (N 'English level 4', N 'four', N 'six')
 
 
--STR function: Converts the value of a numeric expression to a string, and automatically rounds when converting. The default length is 10, if no decimal place is specified, it is reserved to integer digits. If the length is less than the number of integer digits in the value of the numeric expression, a string of * signs will be returned.
print '(0) 123456789'
print '(1)' + str (123.456,7,3)
print '(2)' + str (123.456,3,0)
print '(3)' + str (123.456,2, 0)
print '(4)' + str (123.456,9,2)
print '(5)' + str (123.456,10,5)
print '(6)' + str (123.456)
 
 
Mathematical functions
 
ABS ( numerical expression ) absolute value function, returns the absolute value of the numeric expression
ACOS ( Real expression inverse cosine function, returns the angle corresponding to a cosine value. The angle is expressed in radians, and the value of the real expression ranges from -1 to 1 , if the parameter exceeds this range, the function returns NULL and reports an error
SIN ( numeric expression ) sine function, returns the sine value of the specified angle expressed in radians in the expression
COS ( numeric expression ) cosine function, returns the cosine of the specified angle in radians in the expression
TAN ( numeric expression ) tangent function, returns the tangent of the specified angle in radians in the specified expression
EXP ( numeric expression ) exponential function, returns the exponential value of the expression
LOG ( numerical expression ) natural logarithm function, returns the natural logarithm value of the expression
PI () pi function, returns a 14 -bit decimal pi constant value
RAND () random function, randomly returns the float value between 0 ~ 1
SIGN ( numeric expression ) sign function, returns the positive sign, zero or negative sign of the expression
SQRT ( numeric expression ) square root function, returns the square root of the expression

select N 'Absolute Value Function' = ABS (-5.6), N'Pi
          Function '= PI (),
          N'Square Root Function' = SQRT (64), N'Random
          Function '= RAND (), N'Log
          Function LOGIO '= LOG10 (100),
          N' natural logarithmic function LOG (2.71828) '= LOG (2.71828), N'exponential
          function' = LOG (EXP (2.71828))

 

Data type conversion function

--SQL Server will automatically complete the conversion of data types, this conversion is called implicit conversion. But some types cannot be converted automatically, such as int integer and char type, at this time you must explicitly convert the function cast, convert
--CAST (expression AS data type [(length)]) CONVERT (data type [(length) ], Expression [, style])
--Length: If the data type allows the length to be set, you can set the length, such as varchar (10);
--Style: the style used to convert date type data to character data type date format .

select CAST(1314.521+6 as int),CONVERT(int,521.1314)

 

Date and time functions

GETDATE ( date-time data ) returns the DATETIME value of the local server in a standard format
DAY ( date-time data ) returns the date value of the specified date
MONTH ( date-time data ) returns the month value of the specified date
YEAR ( date-time data ) returns the year value of the specified date
The DATEADD ( interval, numerical expression type, date ) as DATETIME addition interval value. Time interval decision unit interval, it is desirable Year , Day of year ( the number of days a year ) , Quarter , Month , Day , Week , (Weekday week days ) , Hour , Minute , Second , Millisecond . number
Value expression is the time interval of addition or subtraction
The DATEDIFF ( interval, date 1, date 2) calculates the difference between the two specified date specified time interval, the number of return value data type. Time interval decision unit time interval, the value of DATEADD same time interval item
 
--Example: Use the date and time function to display the current date, a date 10 days after the current date, the number of days between the current date and April 01, 2020
select N 'displays the current system date' = GETDATE (),
          N 'date 10 days after the current date' = DATEADD (day, 10, GETDATE ()),
          N 'number of days between the current date and April 1, 2020 '= DATEDIFF (DAY, GETDATE (),' 2020-4-01 ')
 
System function
 
--Users can obtain the current host name, user name, database name and system error information through system functions when needed.
-(1) The function HOST_NAME () returns the name of the server computer.
-(2) The function OBJECT_NAME () returns the name of the database object.
-(3) The function USER_NAME () returns the database user name.
-(4) The function SUSER_NAME () returns the user login name-
(5) The function DB_NAME () returns the database name
 
select 'Computer name' = HOST_NAME (),
          'Login name' = SUSER_NAME (),
          'Database name' = DB_NAME (),
          'Database user name' = USER_NAME ()
 
There are a lot of functions in sqlserver, so I wo n’t introduce them here. If you want to know more functions, you can go to Baidu. I am also in the learning stage. It is impossible to use all the functions. For everyone, I hope to help everyone learn.
 
Published 105 original articles · Like 536 · Visits 70,000+

Guess you like

Origin blog.csdn.net/qq_41934990/article/details/105565740