当前位置:网站首页>SQL server functions
SQL server functions
2022-06-26 05:57:00 【weixin_ forty-seven million two hundred and fifty-four thousand】
Writing time :2022 year 6 month 8 Japan
SQL Server function
SQL Contains the following types of functions :
1、 Aggregate functions : Returns the summary value .
2、 Type conversion function : Convert one data type to another .
3、 Date function : Processing date and time .
4、 Mathematical functions : Perform arithmetic operations .
5、 String function : The string 、 Binary data or expressions perform operations .
6、 System function : Return from database at SQL Server The value in 、 Special information about objects or settings .
One 、 Aggregate functions :
It returns a value for each rowset it applies .AVG( expression ) Returns the average of all values in the expression . Only for numeric columns and automatically ignore NULL value . Returns the average price for each product category :
SELECT
category_name,
CAST(ROUND(AVG(list_price),2) AS DEC(10,2))
avg_product_price
FROM production.products p
INNER JOIN production.categories c
ON c.category_id = p.category_id
GROUP BY
category_name
ORDER BY
category_name;
COUNT( expression ) Return non in expression NULL The amount of value . Can be used for numeric and character columns . Count the number of customer calls
select COUNT(phone) from sales.customers
COUNT(*) Returns the number of rows in the table ( It includes NULL Columns of values ). Return the number of each brand and product . Besides , It only returns a product quantity greater than 20 The brand of :
SELECT
brand_name,
COUNT() product_count
FROM production.products p
INNER JOIN production.brands c
ON c.brand_id = p.brand_id
GROUP BY brand_name
HAVING
COUNT() > 20
ORDER BY
product_count DESC;
MAX( expression ) Returns the maximum value in the expression , Ignore NULL value . Can be used for digital 、 Character and date time Columns . The following statement gets the brand name and maximum price of each brand :
SELECT
brand_name,
MAX(list_price) max_list_price
FROM production.products p
INNER JOIN production.brands b
ON b.brand_id = p.brand_id
GROUP BY
brand_name
ORDER BY
brand_name;
Two 、 Type conversion function :
Put the string ’100.125456’ Convert to Numeric type , Retain 2 Decimal place
select CONVERT(numeric(10,2) ,‘100.125456’);
Put the string ’100.125456’ Convert to Numeric type , Retain 3 Decimal place
select cast(‘100.125456’ as numeric(10,3))
String to time , Year of return
select year(cast(‘2022-03-13 12:00:30’ as date))
3、 ... and 、 Date function :
Because the date cannot directly execute the arithmetic function , So the date function is very useful .GETDATE() Current system date .
select GETDATE() -- result :2019-05-07 18:34:27.343
DATEADD( Date part ,number,date) Returns a number with a specified number (number) Date (date), This number is added to the specified date section (datepart)
select DATEADD(dd, 5, getdate()) -- increase 5 Days time
DATEDIFF( Date part ,date1,date2) Returns the difference between the specified date parts of two dates .
select DATEDIFF(mm, ‘2010-1-1’, ‘2010-3-1 00:00:00’) -- result :2
DATENAME( Date part ,date) Returns the string form of the date part of the date .
select DATENAME(dw,GETDATE()) -- result : Tuesday
notes :DATENAME and DATEPART The difference between , The returned value types are different , One is VARCHAR One is INT, In addition, the week will be expressed in local language
Four 、 Number function :
Perform algebraic operations on numeric values .
ABS(num_expr) Returns the absolute value of a numeric expression .
select ABs(-1.2) -- Return the absolute value 1.2
FLOOR(num_expr) Returns the largest integer less than or equal to a numeric expression
select floor(1.2) -- return 1
CEILING(num_expr) Returns the smallest integer greater than or equal to a numeric expression
select CEILING(1.2) -- return 2
RAND([seed]) Random return to 0-1 Approximate floating point values between , It can be done to seed Specify as an integer expression ( Optional ).
select round(rand()*40,0)+60 -- Random generation 60-100 The integer of
ROUND(num_expr,length) Intercepts the specified integer length for a numeric expression , Returns the rounded value .
select ROUND(list_price,1) from production.products
5、 ... and 、 String function :
Can be used for binary and varbinary Data type column , But mainly for char and varchar data type .Expr1+expr2 Returns a string in the combined form of two expressions . Be careful int type
select ‘b’+‘a’;
select ‘b’+1;
LEN(char_expr) Returns the length of a character expression .
select len(‘123456’)
LOWER(char_expr) Convert all character expressions to lowercase .
select LOWER(‘ABc’)
LTRIM(char_expr) Returns a character expression that removes the preceding space .
select LTRIM(’ abc d e’)
6、 ... and 、 System function :
Used to return metadata or configuration settings .COALESCE(expr1,expr2, xprN) Return the first non NULL expression .
select coalesce(null,1,1+1)
select coalesce(2,null,1+2)
select coalesce(null,null,1+2)
DATALENGTH(‘expr’) Returns the actual length of any type of data .
select DATALENGTH(‘ in ’); --2 byte
select DATALENGTH(‘A’); – 1 byte
ISNULL(expr,value) Replace with the specified value NULL expression .
select isnull(phone,‘unknow’) from sales.customers;
NULLIF(expr1,expr2) Expr1 And Expr2 When equal , return Null, Otherwise, the first expression is returned .
select nullIf(len(first_name),len(last_name)) from sales.customers
row_number Pagination row_number Has a very wide range of uses , It can be used to achieve web Paging of programs , It will generate a sequence number for each row of records queried , Sort in order and don't repeat , Pay attention to row_number Function must use over Clause to select a column , Or several columns can be sorted to generate sequence numbers
SELECT * FROM(
SELECT row_number() OVER(
ORDER BY list_price )
AS rowNum, product_id, product_name, list_price
FROMproduction.products ) a
WHERE rowNum > (3 -1) * 10
AND rowNum <= 3 * 10
边栏推荐
猜你喜欢

Detailed explanation of serial port communication principle 232, 422, 485

Factory method pattern, abstract factory pattern

适配器模式

Vs2022 offline installation package download and activation

Record how to modify the control across threads

421- binary tree (226. reversed binary tree, 101. symmetric binary tree, 104. maximum depth of binary tree, 222. number of nodes of complete binary tree)

There are applications related to web network request API in MATLAB (under update)
Explore small program audio and video calls and interactive live broadcast from New Oriental live broadcast

家庭记账程序(第一版)

DOM文档
随机推荐
家庭记账程序(第一版)
RIA想法
Cyclic displacement
操作符的优先级、结合性、是否控制求值顺序【详解】
售前分析
小程序第三方微信授权登录的实现
05. basic data type - Dict
423- binary tree (110. balanced binary tree, 257. all paths of binary tree, 100. same tree, 404. sum of left leaves)
5分钟包你学会正则表达式
冒泡排序(Bubble Sort)
There are applications related to web network request API in MATLAB (under update)
kolla-ansible部署openstack yoga版本
Application of cow read / write replication mechanism in Linux, redis and file systems
怎么把平板作为电脑的第二扩展屏幕
机器学习 05:非线性支持向量机
【C語言】深度剖析數據在內存中的存儲
Yamaha robot splits visual strings
项目中止
适配器模式
Life is so fragile