Transact—SQL内置函数

查询编辑器中不区分大小写

1、数学函数

  1、select ceiling {54。57}(大于或等于54.57的最小整数)

  2、select floor {54.57}(返回小于或等于的最大整数)

  3、select square(ceiling(rand()*10))(求出一个0-10的随机整数,并计算其平方)

2、字符串函数

  1、ascll函数(返回字符串表达式最左端字符的ASCll码值)

    select ASCII (‘happy’)(返回值为104,h的ascll值)

  2、char函数(把acsll码值换算成对应的字符)

    select char(104)(返回值为‘h’)

  3、charindex(返回字符串中指定表达式的开始位置,三个参数分别是搜索串、查找串、指定起始位置)如果不指定或者指定为负值,搜索从指定位置开始。

    select charindex(‘a’,‘i have many friends’)返回4

    select charindex(‘a’,‘i have many friends’,5)返回9

  4、left函数(返回字符串左边开始指定个数的字符串,两个参数,字符串或者二进制表达式,指定返回字符串的长度)如果指定值为负数或0,返回空值

    select left(‘i have many friends’,6)返回 i have 

    select left(‘i have many friends’,20)返回i have many friends,长度超过不会出错,返回整个字符串

  5、right(同left从后取)

    select left(‘i have many friends’,7)返回friends

  6、len(返回指定字符串总长度)

    select len(‘朋友’) 返回值2

  7、ltrim(用于去除指定字符串左边的空格)

    select ltrim(‘   there are two space in the left’)返回值 there are two space in the left,只能去除空格去除不掉TAB

  8、rtrim(去除治党字符串右边的空格)

  9、lower(把指定大写字符转换为小写字符)

  10、 upper(把指定字符串中的小写字符转换成相应的大写字符)

  11、patindex函数(返回指定表达式中某模式的初始位置,没有匹配字符串,就返回0)

    patindex(%pattern%,expression)

    select patindex(%in%,'there are many tree in my garden')返回值为21

  12、replace函数(用于在一个字符串中用指定的字符串替换另一个字符串)

    select replace(‘he is a student’,‘student’,‘teacher’)返回值he is a teacher 如果3个参数中任意一个参数为NULL,整个语句的返回值为NULL

  13、reverse函数(用于返回表达式的逆向形式)

    select reverse(‘a fine day’)返回yad enif a

  14、str函数(将数字数据转换为字符数据)

    select str(23.456,6,3)返回字符串为‘23.456’

    select str(23.456)默认返回不保留原数据的小数位

  15、substring(用于获取指定字符串或二进制数据的一部分)

    select substring(‘i have a dream’,1,6)返回i have

3、日期和时间函数

  1、dateadd(用于返回指定日期加上一个时间段后的新日期时间值)

    dateadd(datepart,number,date)

    daypart year、month、day、hour、minute、second、millsecond

  2、datename(用于返回指定日期指定部分的字符串)

    select datename(weekday,‘2023-10-10’)返回值为星期日

  3、datepart函数(用于返回指定日期指定部分的整数值)

    select  datepart(weekday,‘2023-10-10’)返回值1

  4、day(用于返回指定日期的天数)

    select day(‘2023-10-10’) 返回10

  5、month(用于返回指定日期的月份)

    select month(2023-20-20)返回10

  6、year(返回指定日期的年份)

  7、getdate(用于返回系统的日期和时间)

    select getdate()返回当前的系统日期和时间

    select day(getdate())使用day获取当前的天数值

4、数据类型转换函数

  1、cast(用于将某种类型的数据转换为另一种类型的数据)

    select cast(‘010101’ as datetime)返回2001-01-01 000000000

    select cast(‘010101’ as float)返回10101

  2、convert(用于按照指定格式将数据转换成另一种类型的数据)

    select convert(varchar(12),getdate()),返回05 31 2014

5、聚合函数

  1、avg(计算班级中学生的平均年龄)

    select avg(stuage)from studentINfo返回22

    select avg(distinct stuage)from studentinfo 返回23,distinct去除数据中的重复值

  2、count(用于计算总数)

    select count(stuid)from studentinfo

    select count(*)from studentinfo

  3、max函数(返回计算组数据中的最大值)

    select max(stuage)from studentinfo

  4、min函数(计算组数据中的最小值)

  5、sum(计算表达式中所有数据的和)

    select avg(stuage)from studentinfo 返回22

    select sum(stuage)/ count(*)from studentinfo 返回22

6、系统函数

  1、serverproperty(用于查询有关服务器实例的属性信息)

    select convert(char(15),serverproperty(‘servername’))返回值为当前服务器所使用的的实例名称

  2、datebaseproperty(用于查询指定数据库和属性名的命名数据库属性值)

    select databaseproperty(‘master’,‘IsTruncLog’)返回值为1,表示该熏香设置成了true

  3、typeofproperty(用于查询有关数据类型的信息)

    select typeofproperty(‘int’,PRECISION)返回int的preision属性,返回值为10

  4、object_id(用于查询数据库对象的id)

    select object_id(‘dbo.studentinfo’,‘u’)查询studentinfo的id,返回值为2073058421,表不存在就返回null

7、文本和图像函数

  1、textptr(返回对应varbinary格式的text、ntext或者image列的文本指针值)查找到的文本指针值可应用于READTEXT、writetxt和updatetext语句

    select textptr(stuadd) from studentinfo 查询表中的stuadd的十六字节文本指针

  2、textvalid(用于检查特定文本指针是否为有效的text、ntext或image函数)

  select stuid,‘valid(if 1)text data’ = textvalid(‘studentinfo.stuadd’,textptr(stuadd))是否存在用于sudentinfo表的stuadd列中的各个值得有效文本指针

 

    

    

posted @   风中凌乱的猪头  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示