常用函数
1 select LEN('哈哈hello') --返回字符的个数 2 select datalength('哈哈hello') --返回时字节个数 3 select LOWER('AaBb') 4 select upper('AaBb') 5 select '======'+RTRIM(ltrim(' aaa '))+'=======' 6 --从左边数,截取10个字符 7 select left('hello welcome to china.',10) 8 --从右边数,截取10个字符 9 select right('hello welcome to china.',10) 10 --从索引为2的开始,截取5个字符 11 --注意:1.索引从1开始数,2.含当前索引 12 select SUBSTRING('abcdefghigklmn',2,5) 13 14 select REPLACE('fjdakdsfaksdjfa;kdjfqwenc','f','☆') 15 select REPLACE(username,'赵','李')
日期函数
--sql2005只有datetime类型,2008中有datetime,datetime(2)等类型 select getdate() print convert(varchar(50),getdate(),111) print dateadd(day,200,getdate()) update TblStudent set tsBirthday='2012-2-2' where tsBirthday>GETDATE() --请查询出所有入职一年以上的 select * from TblStudent where dateadd(YEAR,1,tsBirthady)<GETDATE() --计算时间之差 datediff(year,'1990-9-9',getdate()) --计算每个年份出生的人数 select YEAR(tsBirthday) 出生年份 count(*) 人数 from Mystudent group by YEAR(tsBirthday) select YEAR(getdate()) select month(getdate()) select day(getdate()) select DATEPART(year,getdate()) select DATEPART(MONTH,getdate()) select DATEPART(DAY,getdate()) select DATEPART(HOUR,getdate()) select DATEPART(MINUTE,getdate()) select DATEPART(SECOND,getdate()) select DATEPART(MS,getdate()) --限制是否为同一个月份 datediff(month,StartDateTime,'2013-7-12')=0
浙公网安备 33010602011771号