1、 字符串函数:
charindex(str1,str2,[起始位置]) 返回值:int,返回str1 在str2中的起始位置,找不到,返回0;
select charindex(‘h’,’hpy’) 显示值为1;
len:返回长度 select len('hpy') 显示值为3;
lower 把字符串转换成小写。Select lower('HPY'); 显示为hpy
upper转换成大写 Select upper('hpy') 显示为HPY
ltrim ,rtrim 去除空格 select ltrim(' hpy ') ‘hpy ‘
select RTRIM(' hpy ') ‘ hpy’
right 从右边起提取字符select right('huangpeiyan',3); yan
select right('黄培燕',2); 培燕
replace select replace('huangpeiyan','yan','zheng'); huangpeizheng
stuff 删除指定长度的字符,并在该位置插入一个新的字符
select stuff('huangpeiyan',6,3,'xiao'); huangxiaoyan
2.日期函数
select getdate(); 返回当前日期 2008-10-11 19:48:42.653
select dateadd(mm,1,getdate());2008-11-11 19:47:36.123
select dateadd(dd,1,getdate());2008-10-12 19:47:36.123
select dateadd(yy,1,getdate());2009-10-11 19:47:36.123
datediff 两个日期之间指定日期部分的区别
select datediff(mm,'08/01/1999','03/03/1999') -5
select datediff(dd,'03/01/1999','03/03/1999') 2
select datediff(yy,'03/01/1999','03/03/2005') 6
datename 指定日期部分的字符串。select datename(dw,'01/01/2000'); 星期六
select datepart(day,'03/28/1999') 28
select datepart(mm,'03/28/1999') 3
select datepart(yyyy,'03/28/1999') 1999
3.数学函数
ABS 取绝对值 select abs(-33); 33
Ceiling 向上取整
select ceiling(3.1);
select ceiling(3.9); 两者均显示为4;
floor向下取整 select floor(3.1); 3
power取幂:
select power(5,2);25
round 四舍五入 两个参数,后面那个指定了精度
select round(4.1,0);
select round(4.5,0);
sign 正数返回+1,负数返回1,0返回0;
select sign(4);
select sign(-4);
select sign (0);
sqrt取平方根 select sqrt(4); 2
4.系统函数:
Convert 用来转换数据类型 select convert(varchar(5),134);
select current_user; --当前用户名字dbo
select user_name(1); --给定用户ID返回用户名dbo
select system_user; --当前登录所用名称sa
select suser_sname(); --sa
select datalength('huangpeiyan');--返回表达式字节数11
select host_name() --所登陆计算机的名字 33DFB0EC6EA5486
注:select datalength('黄培燕'); --6
select len('黄培燕'); --3
5.聚合函数
Sum select sum(score) from student; --某一列丛向求和。
AVG Select avg(score) from studet; --求平均数
MAX Select MAX(score) from studet; --求最大值
MIN Select MIN(score) from studet; --求最小值
COUNT select count(*) from student where score>=60 –计算行数