sql函数大全

-- 聚合函数
use pubs
go
select avg ( distinct price) -- 算平均数
from titles
where type = ' business '
go
use pubs
go
select max (ytd_sales)  -- 最大数
from titles
go

use pubs
go
select min (ytd_sales) -- 最小数
from titles
go

use pubs
go
select type, sum (price), sum (advance)  -- 求和
from titles
group by type
order by type
go

use pubs
go
select count ( distinct city)  -- 求个数
from authors
go

use pubs
go
select stdev (royalty) -- 返回给定表达式中所有值的统计标准偏差
from titles
go

use pubs
go
select stdevp (royalty) -- 返回表达式中所有制的填充统计标准偏差
from titles
go

use pubs
go
select var (royalty) -- 返回所有值的统计方差
from titles
go

use pubs
go
select varp (royalty) -- 返回所有值的填充的统计方差
from titles
go


-- 数学函数

select sin ( 23.45 ), atan ( 1.234 ), rand (), PI (), sign ( - 2.34 ) -- 其中rand是获得一个随机数
--
配置函数
SELECT @@VERSION -- 获取当前数据库版本
SELECT @@LANGUAGE -- 当前语言
--
时间函数
select getdate () as ' wawa_getdate ' -- 当前时间
select getutcdate() as ' wawa_getutcdate ' -- 获取utc时间
select day ( getdate ()) as ' wawa_day ' -- 取出天
select month ( getdate ()) as ' wawa_month ' -- 取出月
select year ( getdate ()) as ' wawa_year ' -- 取出年
select dateadd (d, 3 , getdate ()) as wawa_dateadd -- 加三天,注意'd'表示天,'m'表示月,'yy'表示年,下面一样
select datediff (d, ' 2004-07-01 ' , ' 2004-07-15 ' ) as wawa_datediff -- 计算两个时间的差
select datename (d, ' 2004-07-15 ' ) as wawa_datename -- 取出时间的某一部分
select datepart (d, getdate ()) as wawa_datepart  -- 取出时间的某一部分,和上面的那个差不多
--
字符串函数
select ascii ( 123 ) as ' 123 ' , ascii ( ' 123 ' ) as ' "123" ' , ascii ( ' abc ' ) as ' "abc" ' -- 转换成ascii码
select char ( 123 ), char ( 321 ), char ( - 123 ) -- 根据ascii转换成字符
select lower ( ' ABC ' ), lower ( ' Abc ' ), upper ( ' Abc ' ), upper ( ' abc ' ) -- 转换大小写
select str ( 123.45 , 6 , 1 ), str ( 123.45 , 2 , 2 ) -- 把数值转换成字符串
select ltrim ( '      "左边没有空格" ' -- 去空格
select rtrim ( ' "右边没有空格"     ' ) -- 去空格
select ltrim ( rtrim ( '     "左右都没有空格"    ' )) -- 去空格
select left ( ' sql server ' , 3 ), right ( ' sql server ' , 6 ) -- 取左或者取右

use pubs
select au_lname, substring (au_fname, 1 , 1 ) -- 取子串
from authors
order by au_lname

select charindex ( ' 123 ' , ' abc123def ' , 2 ) -- 返回字符串中指定表达式的起始位置
select patindex ( ' 123 ' , ' abc123def ' ), patindex ( ' %123% ' , ' abc123def ' ) -- 返回表达式中某模式第一次出现的起始位置
select quotename ( ' abc ' , ' { ' ), quotename ( ' abc ' ) -- 返回由指定字符扩住的字符串
select reverse ( ' abc ' ), reverse ( ' 上海 ' ) -- 颠倒字符串顺序
select replace ( ' abcdefghicde ' , ' cde ' , ' xxxx ' ) -- 返回呗替换了指定子串的字符串
select space ( 5 ), space ( - 2 )

转载于:https://www.cnblogs.com/mayiyi-01/archive/2012/11/20/2778848.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值