![SQL Server数据库应用基础(第2版)](https://wfqqreader-1252317822.image.myqcloud.com/cover/302/654302/b_654302.jpg)
3.5 T-SQL常用函数的使用
3.5.1 数学函数
数学函数用于对数字表达式进行数学运算并返回运算结果。常用的SQL Server数学函数如表3.13所示。
表3.13 常用的SQL Server数学函数
示例3.11
select round(534.56,1), round(534.56,0), round(534.56,-1), round(534.56,-2) select round(534.5645,3),round(534.5645,3,1),round(534.5645,3,3) declare @abc bigint,@xyz bigint set @abc=round(534.56,-3) set @xyz=round(534.56,-4) select @abc, @xyz
执行结果如图3.12所示。
![](https://epubservercos.yuewen.com/651175/3590268603411501/epubprivate/OEBPS/Images/Figure-0077-01.jpg?sign=1738801639-wj8JGvnd77FpPddJVyzzdYEQXVr3SZKh-0-947faffb5196b07aaf3038bf22f08153)
图3.12 数学函数round的使用
注意点:
将示例3.11中的语句set @abc=round(534.56,-3) set@xyz=round(534.56,-4)改为select round(534.56,-3),round(534.56,-4)后,在执行批处理时会出现错误提示:“消息8115,级别16,状态2,第4行将expression 转换为数据类型numeric 时出现算术溢出错误。”
3.5.2 日期和时间函数
日期和时间函数用于对日期和时间数据进行各种不同的处理和运算,并返回一个字符串、数字值或日期和时间值。常用的SQL Server日期和时间函数如表3.14所示,日期类型的名称及其可操作值如表3.15所示。
表3.14 常用的SQL Server日期和时间函数
表3.15 日期类型的名称及其可操作值
示例3.12
select getdate() 当前日期,datepart(yy,getdate()) 年, datename(mm,getdate())月,datepart(dd,getdate()) 日, datepart(wk,getdate()) 全年第多少周,datepart(dw,getdate()) 星期几
执行结果如图3.13所示。
![](https://epubservercos.yuewen.com/651175/3590268603411501/epubprivate/OEBPS/Images/Figure-0078-02.jpg?sign=1738801639-cMmU7ZPRtBNREsu9XH2T4bzKRqB81zLX-0-e701b837f2005aec27d7b5bfd34a4f7f)
图3.13 日期函数的使用
3.5.3 聚合函数
聚合函数用于对一组值进行计算并返回一个单一的值。除count(*)函数以外,聚合函数忽略空值。聚合函数经常与select语句的group by子句一同使用。仅在下列项中聚合函数允许作为表达式使用,即select语句的选择列表(子查询或外部查询)、compute或compute by子句、having子句。
常用的SQL Server聚合函数如表3.16所示。
表3.16 常用的SQL Server聚合函数
注:*仅做了解。
3.5.4 字符串函数
字符串函数可以对二进制数据、字符串和表达式执行不同的运算,大多数字符串函数只能用于char和varchar数据类型,通常能明确转换成char和varchar的数据类型,少数几个字符串函数也可用于binary和barbinary数据类型。此外,某些字符串函数还能够处理text、ntext、image数据类型的数据。常用的字符串函数如表3.17所示。
表3.17 常用的字符串函数
示例3.13
select charindex(‘cde’, ‘abcdefg’,2) 子串位置,left(‘abcdefg’,3) 取左子串, right(‘abcdefg’,3) 取右子串,substring(‘abcdefg’,3,4)取子串, len(‘abcdefg’) 串长,upper(‘abcdefg’) 小写转大写 select ltrim(‘ abcdefg ’) 去左空格,rtrim(‘ abcdefg ’) 去右空格, replace(‘abcdefg’, ‘abc’, ‘ABCD’) 替换子串,str(123.456,8,2) 数字转字符, convert(float, ‘123.456’) 字符转数字
执行结果如图3.14所示。
![](https://epubservercos.yuewen.com/651175/3590268603411501/epubprivate/OEBPS/Images/Figure-0080-01.jpg?sign=1738801639-hZX5EWqBRRhgkHskMzMge3nxYAZDh5ep-0-056fe7e0df4f1355f10ad05daefe8258)
图3.14 字符串函数的使用
3.5.5 转换函数
一般情况下,SQL Server会自动处理某些数据类型的转换。例如,如果比较char和datetime表达式、smallint和int表达式,或不同长度的char表达式,SQL Server可以将它们自动转换,这种转换被称为隐式转换。但是,无法由SQL Server自动转换或由SQL Server自动转换的结果不符合预期结果时,就需要使用转换函数进行强制转换。转换函数有两个,即convert()和cast(),如表3.18所示。
表3.18 转换函数
其中,style选项能以不同的格式显示日期和时间。如果将datetime或者smalldatetime转换为字符数据,style用于给出转换后的字符格式,style参数的取值表如表3.19所示。
表3.19 style参数的取值表
示例3.14
declare @myval decimal (5, 2) set @myval = 193.57 select cast(cast(@myval as varbinary(20)) as decimal(10,5)) cast转换成十进制数, convert(decimal(10,5), convert(varbinary(20), @myval)) convert转换成十进制数 select convert(char,getdate()) 默认转换, convert(char,getdate(),1) ‘1’, convert(char,getdate(),2) ‘2’, convert(char,getdate(),3) ‘3’, convert(char,getdate(),4) ‘4’, convert(char,getdate(),5) ‘5’, convert(char,getdate(),6) ‘6’ select convert(char,getdate(),7) ‘7’, convert(char,getdate(),8) ‘8’, convert(char,getdate(),9) ‘9’, convert(char,getdate(),100) ‘100’,convert(char,getdate(),101) ‘101’ select convert(char,getdate(),109) ‘109’
执行结果如图3.15所示。
![](https://epubservercos.yuewen.com/651175/3590268603411501/epubprivate/OEBPS/Images/Figure-0081-02.jpg?sign=1738801639-0JtubCb4jRnURYMvUWT1PxMBlpPwr6QA-0-3e0d1a69e189c3942fbd3c384314148f)
图3.15 转换函数的使用
3.5.6 其他系统函数
系统函数除convert()与cast()以外,还有如表3.20所示的几个常用的系统函数。
表3.20 常用的系统函数
示例3.15
select current_user 当前用户, dataLength(‘中华人民共和国’) 字节数, host_name() 计算机名称, system_user 当前登录用户名, user_name() 根据用户ID返回的用户名
执行结果如图3.16所示。
![](https://epubservercos.yuewen.com/651175/3590268603411501/epubprivate/OEBPS/Images/Figure-0082-02.jpg?sign=1738801639-QBZYO3tq97Sput3OfCSe3366Lox0gRTp-0-a45a5e5b889462819fc70f64f74923f4)
图3.16 系统函数的使用