SQL自带的一些函数

Copy 以下内容到查询分析器操作:


-- 3 ****************************************************
-- 熟悉系统自带的一些函数

-- 3.1 --------------------------------------------------
-- 数学函数

-- ABS(num_expression)          返回给定数值表达式的绝对值
-- CEILING(num_expression)      返回大于或等于给定数值表达式的最小整数
-- ROUND(num_expression,length) 返回四舍五入到指定长度或精度的数值表达式(当 length 为正时,四舍五入为 length 所指定的小数位数;当 length 为负时,在小数点的左边四舍五入)
-- /                            取模
-- %                            取余
-- RAND()                       获得值为 0-1 之间的随机浮点值

select abs(5),abs(-5)
select ceiling(3.14),ceiling(3)
select round(748.981,-4) as '-4',round(748.981,-3) as '-3',round(748.981,-2) as '-2',round(748.981,-1) as '-1'
      ,round(748.981,0) as '0',round(748.981,1) as '1',round(748.981,2) as '2',round(748.981,3) as '3'
select 7/3
select 7%3
select rand()

-- 3.2 --------------------------------------------------
-- 聚合函数

-- MIN()    求最小值
-- AVG()    求平均值
-- MAX()    求最大值
-- SUM()    求和
-- COUNT(*) 求选定的行数

select min(UnitPrice) as min,avg(UnitPrice) as avg,max(UnitPrice) as max from Northwind..Products
select count(*) from Northwind..[Order Details] where OrderID = 10248

-- 3.3 --------------------------------------------------
-- 字符串函数

-- LEN(string_expression)                     返回字符串长度
-- SUBSTRING(string_expression,start,length)  返回字符串指定位置起的指定长度的字符串
-- LEFT(string_expression,int)                返回字符串左侧起指定数量的字符
-- REVERSE(string_expression)                 返回字符串的反转表达式
-- CHARINDEX(expression_1,expression_2,start) 返回从 start 位置算起,表达式1 在 表达式2 中的位置

select len('International Business Machinery')
select substring('International',6,4)
select left('Shanghai',4)
select reverse('SQL Server')
select charindex('{1}','{1}[1];{1}[2];',5)

-- 3.4 --------------------------------------------------
-- 日期和时间函数

-- GETDATE()                              返回系统当前时间
-- DATEPART(datepart,date)                返回给定时间的指定部分
-- DATEDIFF(datepart,start_date,end_date) 返回给定2个时间之间的差
-- DATEADD(datepart,num,date)             向指定日期添加间隔
-- CONVERT(char(length),date,style)       把日期表达式转换成指定格式的字符串

select getdate()
select datepart(yy,getdate()) as year,datepart(mm,getdate()) as month,datepart(dd,getdate()) as day
       ,datepart(hh,getdate()) as hour,datepart(mi,getdate()) as minute,datepart(ss,getdate()) as second
select datediff(dd,'2005-01-01',getdate())
select dateadd(dd,-7,getdate()) as '7天前',dateadd(dd,7,getdate()) as '7天后'
select getdate()-1 as '昨天',getdate() as '今天',getdate()+1 as '明天'
select convert(char(10),getdate(),120) as 'ODBC 规范',
       convert(varchar,getdate(),110) as '美国',
       convert(varchar,getdate(),111) as '日本',
       convert(varchar,getdate(),112) as 'ISO'


-- 练习 --------------------------------------------------
-- 1 获得 1 - 100 的随机整数
-- 2 获得长度为 10 位的由随机数字组成的字符串
-- 3 查询 Northwind 数据库中 Order Detail 数据表中 产品编号为 11 的产品的销售额是多少
-- 4 查询 Northwind 数据库中 Order 数据表中没有按时送达的订单,并计算耽误的天数。(结果集中应包含 订单号,要求日期,实际装船日期,延误天数)
-- 5 查询 Northwind 数据库中 Order 数据表中,订货日期为 1996年7月 的订单

 

 

 

 

 

 

 

 


-- 答案 --------------------------------------------------
-- 1
select convert(int,rand()*100+1)
-- 2
select substring(convert(varchar,rand()),3,5)+substring(convert(varchar,rand()),3,5)
select substring(str(rand(),12,12),3,10)
-- 3
select sum(UnitPrice*Quantity) from Northwind..[Order Details] where ProductID = 11
-- 4
select OrderID,RequiredDate,ShippedDate,datediff(dd,RequiredDate,ShippedDate) as Delay from Northwind..Orders where ShippedDate > RequiredDate
-- 5
select * from Northwind..Orders where convert(char(6),OrderDate,112)='199607'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值