- select dm as 数值取整前,CEILING(dm) as 数值取整后 from table CEILING() 向上取整如1.2 取整后为2。
- select dm as 数值取整前,FLOOR(dm) as 数值取整后 from table FLOOR() 向下取整如r1.2 取整后为1。
- select dm as 数值取整前,ROUND(dm,n) as 数值取整后 from table ROUND() 四舍五入,n是指保留多少位小数。
- ABS(列名) 取列绝对值。
- RAND() 产生随机数 大于0小于1 产生小数位数是14位。
- UPPER(列名) 转化为大写。
- LOWER(列名) 转化为小写。
- select friendname as 字符串,(LEN(friendname)-LEN(REPLACE(friendname,'i',''))) from tb_String 计算字符出现的次数。
- SUBSTRING(str,index,length) str列名,index 开始截取字符位置,截取字符串长度。
- CONVERT(type,str) type 转化的类型(如varchar(20)),字符串(列名)。
- STUFF(str,index,length,str2) 代替字符,str字符串,index开始代替位置,length代替的长度,str2代替字符(如果是删除字符,写上''就可以了)。
- REPLACE(str,workKey,str2) str 字符串,workKey要代替字符串,str2代替str中的字符。
- select 所在学院,CHARINDEX('学院',所在学院) from tb_Student CHARINDEX()查找字符串出现的索引。
- ISDATE(str) 返回1是彧者0否str字符串,是否日期。
- were 列名 BWTWEEN str AND str 在....之间。
- YEAR(列名) 获取年 MONTH(列名) 获取月份 DAY(列名)获取日期。
- GETDATE() 获取系统日期。
- DATENAME(WEEKDAY,GETDATE()) 获取当前是星期几。DATENAME(DAY,GETDATE()) 获取本月第几天。
- DATADIFF(type,time,time2) 取两个时间之差,type可以是YEAR、MONTH、DAY ,time第一个时间,time2第二个时间。
- DISTINCT 去掉重复数据, 如:select DISTINCT 列名,列名... from table
- UNION 合并多个结果集。
- WITH ENCRYPTION 加密sql
语法:
1.case...end

case when true/false then str when true/false then str .... else str end
2. with cube 汇总

select 所属部门,性别,AVG(工资) as 平均工资 from tb_Employee group by 所属部门,性别 with cube order by 所属部门
3.with rollup 汇总
rollup 运算符生成的结果集类似于cube运算符所生成的结果集。with cube 和with rollup 之间的区别在于cube生成的结果集显示了 所有组合的聚全数据而rollup生成的结果集显示了所选列中值的某一层次结构的聚合数据。

select 所属部门,性别,AVG(工资) as 平均工资 from tb_Employee group by 所属部门,性别 with rollup
4. 笔画排序

select 学生姓名,性别,年龄,所在学院 from tb_Student order by 学生姓名 collate chinese_prc_stroke_cs_as_ks_ws
5. 拼音排序

select 学生姓名,性别,年龄,所在学院 from tb_Student order by 学生姓名 collate chinese_prc_stroke_cs_as
6.COMPUTE 对查询数据的统计

select * from tb_Employee order by 所属部门 compute sum(工资)
7.COMPUTE BY
既能显示分组情况,又能将各组数据统计结果显示出来,利用COMPUTE BY 可以轻松实现。

select * from tb_Employee order by 所属部门 compute avg(工资) by 所属部门
8.唯一索引

//新建一个唯一索引 //indexName 索引名称, //tableName 表名称 //columnName 表中的列名 CREATE UNIQUE INDEX indxeName ON tableName(columnName) //删除一个唯一索引 DROP INDEX indexName
9.聚簇索引(注:一个表中有一个聚簇索引)

//indexName 索引名称 //tableName 表名 //columnName 表中的列名 CREATE CLUSTERED INDEX indexName ON tableName(columnName) //删除 DROP INDEX tableName.indexName
10.查看用户视图信息

//用户视图 select name from sysobjects where xtype='v' //本实例视图 select name,create,refDate from sysobjects where xtype='v'
Access 数据库
1.format() 格式化

select 员工姓名,出生日期 as 格式化之前,format(出生日期,'yyyy年MM月dd日') as 格式化之后 from 员工生日表
2.MID() 字符串提取信息

select 员工姓名,format(出生日期,'yyyy年MM月dd日') as 出生年月日,mid(出生日期,1,7) as 出生年月 from 员工生日表
3.DATEDIFF() 时间相差

select 员工姓名,format(出生日期,'yyyy年MM月dd日') as 出生年月日,datediff('yyyy',出生日期,date()) as 年龄 from 员工生日表