SQLServer基础(四)

 

第五章:函数
5.1、数学函数
     绝对值Abs+指数Power+平方根Sqrt+随机数rand+舍入到最大整数ceil+舍入到最小整数floor+四舍五入round+弧度制转换角质度degrees+radians+正负零符号sign(结果1,-1,0)+余数%+求幂power(5,Fweight)
     select power(FWeight,3),sqrt(Fweight),rand(9527) from T_Person
     ceil天花板,小数去掉,正数整数+1。floor底板,小数去掉,负数-1。
     round两个参数:
5.2、字符串函数
(长度len+小写lower+大写upper+左去空格ltrim+右去空格rtrim+取子字符串substring(字符串,开始位置,长度)+计算子字符串的位置charindex(字符串,子字符串)+从左侧取子字符串left(string,length)+从右侧取子字符串right+字符串替换replace(string,old,new)+得到字符的ASCII码ASCII(string))
5.3、日期时间函数
     取得当前时间:getdate(),2014-02-08 09:35:03.513
      取得当前日期:convert(varchar(50),getdate(),101),02/08/2014
     取得当前时间:convert(varchar(50),getdate(),108),09:40:00
     时间加法运算:dateadd(datepart,number,date)
     日期差额:datediff(datepart,startdate,enddate)
     日期星期几:datename(datepart,date),select FBirthDay,dateaname(weekday,FbirthDay) from T_Person
     取得日期的指定部分:datepart(datepart,date),select FBirthday,datepart(dayofyear,FBirthday) from T_person
5.4、其他函数
     类型转换cast(expression As data_type )
     类型转换convert(date_type,expression)
     空值处理coalesce(expression,value1,value2,..)合并,结合,coalesce将会返回包括expression在内的所有参数中第一个非空表达式。逻辑判断处理。
     简化版的空值处理:isnull(expression,value)
     空值处理另版:nullif(expression1,expression2)相等返回空值,类型为第一个不等,返回第一个值,null也是。
          select FBirthday,FRegDay,nullif(FBirthday,FRegday) from T_person
     case函数,与语言中的用法类似,
          case expression
          when value1 then return value1
          ...
          else default return value
          end
5.5、独有函数
     isdate函数,判断是否是有效日期。
     isnumeric,判断是否是数值类型。
     辅助功能函数:app_name() 得到服务器的名称;current_user 得到当前表的前缀;host_name() 得到数据库的名称。
posted on 2014-02-10 16:12  shadow_飛  阅读(236)  评论(0编辑  收藏  举报

Shadow