`
wfdoublext
  • 浏览: 127034 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类
最新评论

SQL function

    博客分类:
  • SQL
阅读更多

SQL 函数 函数大全

SQL 函数

Abs(number) 取得数值的绝对值。
Asc(String) 取得字符串表达式的第一个字符ASCII 码。
Atn(number) 取得一个角度的反正切值。
CallByName (object, procname, usecalltype,[args()]) 执行一个对象的方法、设定或传回对象的属性。
CBool(expression) 转换表达式为Boolean 型态。
CByte(expression) 转换表达式为Byte 型态。
CChar(expression) 转换表达式为字符型态。
CDate(expression) 转换表达式为Date 型态。
CDbl(expression) 转换表达式为Double 型态。
CDec(expression) 转换表达式为Decimal 型态。
CInt(expression) 转换表达式为Integer 型态。
CLng(expression) 转换表达式为Long 型态。
CObj(expression) 转换表达式为Object 型态。
CShort(expression) 转换表达式为Short 型态。
CSng(expression) 转换表达式为Single 型态。
CStr(expression) 转换表达式为String 型态。
Choose (index, choice-1[, choice-2, ... [, choice-n]]) 以索引值来选择并传回所设定的参数。
Chr(charcode) 以ASCII 码来取得字符内容。
Close(filenumberlist) 结束使用Open 开启的档案。
Cos(number) 取得一个角度的余弦值。
Ctype(expression, typename) 转换表达式的型态。
DateAdd(dateinterval, number, datetime) 对日期或时间作加减。
DateDiff(dateinterval, date1, date2) 计算两个日期或时间间的差值。
DatePart (dateinterval, date) 依接收的日期或时间参数传回年、月、日或时间。
DateSerial(year, month, day) 将接收的参数合并为一个只有日期的Date 型态的数据。
DateValue(datetime) 取得符合国别设定样式的日期值,并包含时间。
Day(datetime) 依接收的日期参数传回日。
Eof(filenumber) 当抵达一个被开启的档案结尾时会传回True。
Exp(number) 依接收的参数传回e 的次方值。
FileDateTime(pathname) 传回档案建立时的日期、时间。
FileLen(pathname) 传回档案的长度,单位是Byte。
Filter(sourcearray, match[, include[, compare]]) 搜寻字符串数组中的指定字符串,凡是数组元素中含有指定字符串,会将它们结合成新的字符串数组并传回。若是要传回不含指定字符串的数组元素,则include 参数设为False。compare 参数则是设定搜寻时是否区分大小写,此时只要给TextCompare 常数或1 即可。
Fix(number) 去掉参数的小数部分并传回。
Format(expression[, style[, firstdayofweek[, firstweekofyear]]]) 将日期、时间和数值资料转为每个国家都可以接受的格式。
FormatCurrency(expression[,numdigitsafterdecimal [,includeleadingdigit]]) 将数值输出为金额型态。
numdigitsafterdecimal 参数为小数字数,includeleadingdigit 参数为当整数为0 时是否补至整数字数。
FormatDateTime(date[,namedformat]) 传回格式化的日期或时间数据。
FormatNumber(expression[,numdigitsafterdecimal [,includeleadingdigit]]) 传回格式化
的数值数据。Numdigitsafterdecimal 参数为小数字数,includeleadingdigit 参数为当整数为0 时是否补至整数字数。
FormatPercent(expression[,numdigitsafterdecimal [,includeleadingdigit]]) 传回转换为百分比格式的数值数据。numdigitsafterdecimal 参数为小数字数,includeleadingdigit 参数为当整数为0 时是否补至整数字数。
GetAttr(filename) 传回档案或目录的属性值。
Hex(number) 将数值参数转换为16 进制值。
Hour(time) 传回时间的小时字段,型态是Integer。
Iif(expression, truepart, falsepart) 当表达式的传回值为True 时执行truepart 字段的程序,反之则执行falsepart 字段。
InStr([start, ]string1, string2) 搜寻string2 参数设定的字符出现在字符串的第几个字符,start 为由第几个字符开始寻找,string1 为欲搜寻的字符串,string2 为欲搜寻的字符。
Int(number) 传回小于或等于接收参数的最大整数值。
IsArray(varname) 判断一个变量是否为数组型态,若为数组则传回True,反之则为False。
IsDate(expression) 判断表达式内容是否为DateTime 型态,若是则传回True,反之则为False。
IsDbNull(expression) 判断表达式内容是否为Null,若是则传回True,反之则为False。
IsNumeric(expression) 判断表达式内容是否为数值型态,若是则传回True,反之则为False。
join(sourcearray[, delimiter]) 将字符串数组合并唯一个字符串,delimiter 参数是设定在各个元素间加入新的字符串。
Lcase(string) 将字符串转换为小写字体。
Left(string, length) 由字符串左边开始取得length 参数设定长度的字符。
Len(string) 取得字符串的长度。
Log(number) 取得数值的自然对数。
Ltrim(string) 去掉字符串的左边空白部分。
Mid(string, start[, length]) 取出字符串中strat 参数设定的字符后length 长度的字符串,若length 参数没有设定,则取回start 以后全部的字符。
Minute(time) 取得时间内容的分部分,型态为Integer。
MkDir(path) 建立一个新的目录。
Month(date) 取得日期的月部分,型态为Integer。
MonthName(month) 依接收的月份数值取得该月份的完整写法。
Now() 取得目前的日期和时间。
Oct(number) 将数值参数转换为8 进制值。
Replace(expression, find, replace) 将字符串中find 参数指定的字符串转换为replace 参数指定的字符串。
Right(string,length) 由字符串右边开始取得length 参数设定长度的字符。
RmDir(path) 移除一个空的目录。
Rnd() 取得介于0 到1 之间的小数,如果每次都要取得不同的值,使用前需加上Randomize 叙述。
Rtrim(string) 去掉字符串的右边空白部分。
Second(time) 取得时间内容的秒部分,型态为Integer。
Sign(number) 取得数值内容是正数或负数,正数传回1,负数传回-1,0 传回0。
Sin(number) 取得一个角度的正弦值。
Space(number) 取得number 参数设定的空白字符串。
Split(expression[, delimiter]) 以delimiter 参数设定的条件字符串来将字符串分割为字符串数组。
Sqrt(number) 取得一数值得平方根。
Str(number) 将数字转为字符串后传回。
StrReverse(expression) 取得字符串内容反转后的结果。
Tan(number) 取得某个角度的正切值。
TimeOfDay() 取得目前不包含日期的时间。
Timer() 取得由0:00 到目前时间的秒数,型态为Double。
TimeSerial(hour, minute, second) 将接收的参数合并为一个只有时间Date 型态的数据。
TimaValue(time) 取得符合国别设定样式的时间值。
Today() 取得今天不包含时间的日期。
Trim(string) 去掉字符串开头和结尾的空白。
TypeName(varname) 取得变量或对象的型态。
Ubound(arrayname[, dimension]) 取得数组的最终索引值,dimension 参数是指定取得第几维度的最终索引值。
Ucase(string) 将字符串转换为大写。
Val(string) 将代表数字的字符串转换为数值型态,若字符串中含有非数字的内容则会将其去除后,合并为一数字。
Weekday(date) 取的参数中的日期是一个星期的第几天,星期天为1、星期一为2、星期二为3 依此类推。
WeekDayName(number) 依接收的参数取得星期的名称,可接收的参数为1 到7,星期天为1、星期一为2、星期二为3 依此类推。

--聚合函数
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) 

--系统函数
select host_name() as 'host_name',host_id() as 'host_id',user_name() as 'user_name',user_id() as 'user_id',db_name() as 'db_name'
--变量的定义使用
--声明局部变量
declare @mycounter int
declare @last_name varchar(30),@fname varchar(20),@state varchar(2) --一下声明多个变量
--给变量赋值
use northwind
go
declare @firstnamevariable varchar(20),
 @regionvariable varchar(30)
set @firstnamevariable='anne' --可以用set,也可以用select给变量赋值,微软推荐用set,但select在选择一个值直接赋值时很有用
set @regionvariable ='wa' 

select lastname,firstname,title  --用声明并赋值过的变量构建一个Select语句并查询
from employees
where firstname= @firstnamevariable or region=@regionvariable
go
--全局变量
select @@version  --返回数据库版本
select @@error  --返回最后的一次脚本错误
select @@identity  --返回最后的一个自动增长列的id 

--while,break,continue的使用
--首先计算所有数的平均价格,如果低于30的话进入循环让所有的price翻倍,
--里面又有个if来判断如果最大的单价还大于50的话,退出循环,否则继续循环,知道最大单价大于50就break出循环,呵呵,
--我分析的应该对吧.
use pubs
go
while (select avg(price) from titles) <$30 
begin
 update titles
  set price=price*2
  select max(price) from titles
  if(select max(price) from titles) >$50
  break
  else
  continue
end
print 'too much for the marker to bear' 

--事务编程经典例子
--begin transaction是开始事务,commit transaction是提交事务,rollback transaction是回滚事务
--这个例子是先插入一条记录,如果出现错误的话就回滚事务,也就是取消,并直接return(返回),如果没错的话就commit 提交这个事务了哦
--上面的那个return返回可以返回一个整数值,如果这个值是0的话就是执行的时候没出错,如果出错了就是一个负数,
--这个return也可以用在存储过程中,可用用 exec @return_status= pro_name来获取这个值
use pubs
go
begin tran mytran
 insert into stores(stor_id,stor_name)
  values('333','my books')
 go
 insert into discounts(discounttype,stor_id,discount)
  values('清仓甩卖','9999',50.00)
 if @@error<>0
  begin
   rollback tran mytran
   print '插入打折记录出错'
   return
  end
commit tran mytran 

--事务处理的保存点示例
--做了事务保存点后可以rollback(回滚)到指定的保存点,不至于所有的操作都不能用
use pubs
go
select * from stores
begin transaction testsavetran
 insert into stores(stor_id,stor_name)
  values('1234','W.Z.D Book')
 save transaction before_insert_data2
 go
 insert into stores(stor_id,stor_name)
  values('5678','foreat Books')
 go
rollback transaction before_insert_data2
select * from stores 

--存储存储过程
use pubs
if exists(select name from sysobjects where name= 'proc_calculate_taxes' and type='P')
 drop procedure proc_calculate_taxes
go
create procedure proc_calculate_taxes (@p1 smallint=42,@p2 char(1),@p3 varchar(8)='char')
as
select *
from titles
--执行过程
EXECUTE PROC_CALCULATE_TAXES @P2='A'

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics