练习用表为operinfo:
在char_expr中返回字符的长度值,忽略尾空(汉字是占两个字节的)
select datalength(name) from operinfo where operid='54321';output:3
返回部分字符串(从第二位开始向后取三位,包括第二位,汉字和其他字符一样处理)
select substring(name,2,3) from operinfo where operid='54321';output:hen
把目标字符串转换成大写形式(小写的函数为lower)
select upper(name) from operinfo where operid='54321';output:CHEN
反写char_expr中的文本
select reverse(name) from operinfo where operid='54321';output nehc
删除头空(如果字符串前面有空格就删掉
)删除尾空位rtrim
rtrim(char_expr)
select ltrim(name) from operinfo where operid='54321';output:chen
返回指定char_expr的开始位置,否则为0
charindex(char_expr,expression)
select charindex(name,'hehechenhehe') from operinfo where operid='54321';output:5
两个参数也可以反了
select charindex('n',name) from operinfo where operid='54321';output:4
通配符
% 匹配任何数量的字符或无字符(可以放在任何位置)
_ 匹配任何单个字符(空间占位符)(注意是下划线)
[] 规定有效范围,或某个"OR"条件
[ABG] A,B,G
[A-C] A,B,C
[A-CE-G] A,B,C,E,F,G
[^ABG] 除了A,B,G
[^A-C] 除了A,B,C
escape子句
用某个转义字符可在搜索字符串时将通配符作为文字来包含。
ANSI-89 SQL 标准定义了escape子句指定某个转义字符
缺省情况下,[]来转义某个通配符,例:
select * from test_tab
where description like "%20[%]%"
语法:
like char_expression escape escape_character
例
select * from test_tab
where description like "%20#%%" escape "#"
+ 可用于串接字符
select au_laname+","+au_fname from authors
数学函数
abs(numeric_expr)
返回指定值的绝对值
ceiling(numeric_expr)
返回大于或等于指定值的最小整数
exp(float_expr)
给出指定值的指数值
floor(numeric_expr)
返回小于或等于指定值的最大整数
pi()
返回常数3.1415926
power(numeric_expr,power)
返回numeric_expr的值给power的幂
rand([int_expr])
返回0-1之间的随机浮点数,可指定基值
round(numeric_expr,int_expr)
把数值表达式圆整到int_expr指定的精度
sign(int_expr)
返回正+1,零0或负-1
sqrt(float_expr)
返回指定值的平方根
SQL SERVER 支持所有标准的三角函数和其他有用的函数
日期函数
getdate()
返回当前的系统日期和时间
datename(datepart,date_expr)
以字符串形式返回date_expr指定部分的值,转换成合适的名字
datepart(datepart,date_expr)
作为整数返回date_expr值的指定部分
datediff(datepart,date_expr1,date_expr2)
返回date_expr2-date_expr1,通过指定的datepart度量
dateadd(datepart,number,date_expr)
返回日期,通过在date_expr上增加指定number的日期部件而产生的
datepart
日期部件 缩写 值范围
年 yy 1753-9999
季度 qq 1-4
月 mm 1-12
每年中的天 dy 1-366
天 dd 1-31
星期 wk 1-54
星期天 dw 1-7(1=sunday)
小时 hh 0-23
分钟 mi 0-59
秒 ss 0-59
毫秒 ms 0-999
例:
select invoice_no,
datediff(dd,date_shipped,getdate())
from invoices
where balance_due>0
转换函数convert
此函数把值从一种类型改变成另一种类型
convert(datetype [(length)],expression)
select "Advance="+convert(char(12),advance)
from titles
日期转换
convert(datetype[(length)],expression,format)
format指定将日期转换为什么格式,有以下值:
没有世纪 有世纪 转换字符串中日期格式
0 or 100 mon dd yyy hh:miAM(or PM)
1 101 mm/dd/yy
2 102 yy.mm.dd
3 103 dd/mm/yy
4 104 dd.mm.yy
5 105 dd-mm-yy
6 106 dd mon yy
7 107 mon dd,yy
8 108 hh:mm:ss
9 or 109 mon dd,yyyy hh:mi:ss:mmmAM(or PM)
10 110 mm-dd-yy
11 111 yy/mm/dd
12 112 yymmdd
系统函数
函数 定义
访问和安全性信息
host_id() 客户进程的当前主机进程ID号
host_name() 客户进程的当前主计算机名
suser_id(["login_name"]) 用户的SQL Server ID号
suser_name([server_user_id]) 用户的SQL Server登录名
user_id(["name_in_db"]) 用户在 数据库 中的ID号
user_name([user_id]) 用户在数据库中的名字
user 用户在数据库中的名字
show_role() 用户的当前活动角色
数据库和对象信息
db_id(["db_name"]) 数据库ID号
db_name([db_id]) 数据库名
object_id("objname") 数据库对象ID号
object_name(obj_id]) 数据库对象号
col_name(obj_id,col_id) 对象的栏名
col_length("objname","colname") 栏的长度
index_col("objname",index_id,key#) 已索引的栏名
valid_name(char_expr) 若char_expr不是有效标识符,则返回0
数据函数
datalength(expression) 按字节返回expression的长度
tsequal(timestamp1,timestamp2) 比较时戳值,若时戳值不匹配,则返回出错消息
isnull()
isnull函数用指定的值代替查询栏或合计中的空值
例:
select avg(isnull(total_order,$0))
from invoices
一个比较复杂的sql语句:
select bankid as cbank,bankid as orgid,sum(pres_debit_num) as presDebitNum,sum(pres_debit_total) as presDebitTotal,sum(pres_credit_num) as presCreditNum,sum(pres_credit_total) as presCreditTotal,sum(acpt_debit_num) as acptDebitNum,sum(acpt_debit_total) as acptDebitTotal,sum(acpt_credit_num) as acptCreditNum,sum(acpt_credit_total) as acptCreditTotal from (select obankid ,branchid,s.bankid,(case when m.dueflag = '2' and m.inoutflag='1' then m.num else 0 end) as pres_debit_num,(case when m.dueflag = '2' and m.inoutflag='1' then m.fee else 0 end) as pres_debit_total,(case when m.dueflag = '1' and m.inoutflag='1' then m.num else 0 end) as pres_credit_num,(case when m.dueflag = '1' and m.inoutflag='1' then m.fee else 0 end) as pres_credit_total,(case when m.dueflag = '2' and m.inoutflag='2' then m.num else 0 end) as acpt_debit_num,(case when m.dueflag = '2' and m.inoutflag='2' then m.fee else 0 end) as acpt_debit_total,(case when m.dueflag = '1' and m.inoutflag='2' then m.num else 0 end) as acpt_credit_num,(case when m.dueflag = '1' and m.inoutflag='2' then m.fee else 0 end) as acpt_credit_total from iccdb.iccs.feesum_sx as m,organinfo as o where orgid=obankid and orglevel='3' and curcode='CNY' and workdate between '20160302' and '20160623' ) as a group by bankid order by bankid
未完待续。。。