关于SQL Server的所有函数(对工作或学习也很有帮助)

BZ比较懒,平时工作Oracle Mysql SQL Server换着用(工作需要)对于使用函数,除了常用的基本上都是用一个度娘一个,所以今天对SQL Server函数进行了一个汇总,希望能帮到你:

1、聚合函数

 1 --max最大值、min最小值、count统计、avg平均值、sum求和、var求方差
 2 
 3 select 
 4     max(age) max_age, 
 5     min(age) min_age, 
 6     count(age) count_age, 
 7     avg(age) avg_age, 
 8     sum(age) sum_age, 
 9     var(age) var_age 
10 from student;

2、日期时间函数

 1 select dateAdd(day, 3, getDate());--加天
 2 select dateAdd(year, 3, getDate());--加年
 3 select dateAdd(hour, 3, getDate());--加小时
 4 --返回跨两个指定日期的日期边界数和时间边界数
 5 select dateDiff(day, '2011-06-20', getDate());
 6 --相差秒数
 7 select dateDiff(second, '2011-06-22 11:00:00', getDate());
 8 --相差小时数
 9 select dateDiff(hour, '2011-06-22 10:00:00', getDate());
10 select dateName(month, getDate());--当前月份
11 select dateName(minute, getDate());--当前分钟
12 select dateName(weekday, getDate());--当前星期
13 select datePart(month, getDate());--当前月份
14 select datePart(weekday, getDate());--当前星期
15 select datePart(second, getDate());--当前秒数
16 select day(getDate());--返回当前日期天数
17 select day('2011-06-30');--返回当前日期天数
18 select month(getDate());--返回当前日期月份
19 select month('2011-11-10');
20 select year(getDate());--返回当前日期年份
21 select year('2010-11-10');
22 select getDate();--当前系统日期
23 select getUTCDate();--utc日期

3、数学函数

1 select pi();--PI函数
2 select rand(100), rand(50), rand(), rand();--随机数
3 select round(rand(), 3), round(rand(100), 5);--精确小数位
4 --精确位数,负数表示小数点前
5 select round(123.456, 2), round(254.124, -2);
6 select round(123.4567, 1, 2);

4、元数据

 1 select col_name(object_id('student'), 1);--返回列名
 2 select col_name(object_id('student'), 2);
 3 --该列数据类型长度
 4 select col_length('student', col_name(object_id('student'), 2)); 
 5 --该列数据类型长度
 6 select col_length('student', col_name(object_id('student'), 1)); 
 7 --返回类型名称、类型id
 8 select type_name(type_id('varchar')), type_id('varchar');
 9 --返回列类型长度
10 select columnProperty(object_id('student'), 'name', 'PRECISION');
11 --返回列所在索引位置
12 select columnProperty(object_id('student'), 'sex', 'ColumnId');

5、字符串函数

 1 select ascii('a');--字符转换ascii值
 2 select ascii('A');
 3 select char(97);--ascii值转换字符
 4 select char(65);
 5 select nchar(65);
 6 select nchar(45231);
 7 select nchar(32993);--unicode转换字符
 8 select unicode('A'), unicode('');--返回unicode编码值
 9 select soundex('hello'), soundex('world'), soundex('word');
10 select patindex('%a', 'ta'), patindex('%ac%', 'jack'), patindex('dex%', 'dexjack');--匹配字符索引
11 select 'a' + space(2) + 'b', 'c' + space(5) + 'd';--输出空格
12 select charIndex('o', 'hello world');--查找索引
13 select charIndex('o', 'hello world', 6);--查找索引
14 select quoteName('abc[]def'), quoteName('123]45');
15 --精确数字
16 select str(123.456, 2), str(123.456, 3), str(123.456, 4);
17 select str(123.456, 9, 2), str(123.456, 9, 3), str(123.456, 6, 1), str(123.456, 9, 6);
18 select difference('hello', 'helloWorld');--比较字符串相同
19 select difference('hello', 'world');
20 select difference('hello', 'llo');
21 select difference('hello', 'hel');
22 select difference('hello', 'hello');
23 select replace('abcedef', 'e', 'E');--替换字符串
24 select stuff('hello world', 3, 4, 'ABC');--指定位置替换字符串
25 select replicate('abc#', 3);--重复字符串
26 select subString('abc', 1, 1), subString('abc', 1, 2), subString('hello Wrold', 7, 5);--截取字符串
27 select len('abc');--返回长度
28 select reverse('sqlServer');--反转字符串
29  
30 select left('leftString', 4);--取左边字符串
31 select left('leftString', 7);
32 select right('leftString', 6);--取右边字符串
33 select right('leftString', 3);
34 select lower('aBc'), lower('ABC');--小写
35 select upper('aBc'), upper('abc');--大写
36 --去掉左边空格
37 select ltrim(' abc'), ltrim('# abc#'), ltrim('  abc');
38 --去掉右边空格
39 select rtrim(' abc    '), rtrim('# abc#   '), rtrim('abc');

6、安全函数

 1 select current_user;
 2 select user;
 3 select user_id(), user_id('dbo'), user_id('public'), user_id('guest');
 4 select user_name(), user_name(1), user_name(0), user_name(2);
 5 select session_user;
 6 select suser_id('sa');
 7 select suser_sid(), suser_sid('sa'), suser_sid('sysadmin'), suser_sid('serveradmin');
 8 select is_member('dbo'), is_member('public');
 9 select suser_name(), suser_name(1), suser_name(2), suser_name(3);
10 select suser_sname(), suser_sname(0x01), suser_sname(0x02), suser_sname(0x03);
11 select is_srvRoleMember('sysadmin'), is_srvRoleMember('serveradmin');
12 select permissions(object_id('student'));
13 select system_user;
14 select schema_id(), schema_id('dbo'), schema_id('guest');
15 select schema_name(), schema_name(1), schema_name(2), schema_name(3);

 

7、系统函数

 1 select app_name();--当前会话的应用程序名称
 2 select cast(2011 as datetime), cast('10' as money), cast('0' as varbinary);--类型转换
 3 select convert(datetime, '2011');--类型转换
 4 select coalesce(null, 'a'), coalesce('123', 'a');--返回其参数中第一个非空表达式
 5 select collationProperty('Traditional_Spanish_CS_AS_KS_WS', 'CodePage');
 6 select current_timestamp;--当前时间戳
 7 select current_user;
 8 select isDate(getDate()), isDate('abc'), isNumeric(1), isNumeric('a');
 9 select dataLength('abc');
10 select host_id();
11 select host_name();
12 select db_name();
13 select ident_current('student'), ident_current('classes');--返回主键id的最大值
14 select ident_incr('student'), ident_incr('classes');--id的增量值
15 select ident_seed('student'), ident_seed('classes');
16 select @@identity;--最后一次自增的值
17 select identity(int, 1, 1) as id into tab from student;--将studeng表的烈属,以/1自增形式创建一个tab
18 select * from tab;
19 select @@rowcount;--影响行数
20 select @@cursor_rows;--返回连接上打开的游标的当前限定行的数目
21 select @@error;--T-SQL的错误号
22 select @@procid;

 

8、配置函数

 1 set datefirst 7;--设置每周的第一天,表示周日
 2 select @@datefirst as '星期的第一天', datepart(dw, getDate()) AS '今天是星期';
 3 select @@dbts;--返回当前数据库唯一时间戳
 4 set language 'Italian';
 5 select @@langId as 'Language ID';--返回语言id
 6 select @@language as 'Language Name';--返回当前语言名称
 7 select @@lock_timeout;--返回当前会话的当前锁定超时设置(毫秒)
 8 select @@max_connections;--返回SQL Server 实例允许同时进行的最大用户连接数
 9 select @@MAX_PRECISION AS 'Max Precision';--返回decimal 和numeric 数据类型所用的精度级别
10 select @@SERVERNAME;--SQL Server 的本地服务器的名称
11 select @@SERVICENAME;--服务名
12 select @@SPID;--当前会话进程id
13 select @@textSize;
14 select @@version;--当前数据库版本信息

9、系统统计函数

 1 select @@CONNECTIONS;--连接数
 2 select @@PACK_RECEIVED;
 3 select @@CPU_BUSY;
 4 select @@PACK_SENT;
 5 select @@TIMETICKS;
 6 select @@IDLE;
 7 select @@TOTAL_ERRORS;
 8 select @@IO_BUSY;
 9 select @@TOTAL_READ;--读取磁盘次数
10 select @@PACKET_ERRORS;--发生的网络数据包错误数
11 select @@TOTAL_WRITE;--sqlserver执行的磁盘写入次数
12 select patIndex('%soft%', 'microsoft SqlServer');
13 select patIndex('soft%', 'software SqlServer');
14 select patIndex('%soft', 'SqlServer microsoft');
15 select patIndex('%so_gr%', 'Jsonisprogram');

10、用户自定义函数

 1 # 查看当前数据库所有函数
 2 
 3 --查询所有已创建函数
 4 select definition,* from sys.sql_modules m join sys.objects o on m.object_id = o.object_id
 5 and type in('fn', 'if', 'tf');
 6  
 7 
 8 # 创建函数
 9 
10 if (object_id('fun_add', 'fn') is not null)
11     drop function fun_add
12 go
13 create function fun_add(@num1 int, @num2 int)
14     returns int
15 with execute as caller
16 as
17     begin
18         declare @result int;
19         if (@num1 is null)
20             set @num1 = 0;
21         if (@num2 is null)
22             set @num2 = 0;
23         set @result = @num1 + @num2;
24         return @result;
25     end
26 go
27 调用函数
28 select dbo.fun_add(id, age) from student;
29  
30 --自定义函数,字符串连接
31 if (object_id('fun_append', 'fn') is not null)
32     drop function fun_append
33 go
34 create function fun_append(@args nvarchar(1024), @args2 nvarchar(1024))
35     returns nvarchar(2048)
36 as
37     begin
38         return @args + @args2;
39     end
40 go
41  
42 select dbo.fun_append(name, 'abc') from student;
43  
44 
45 # 修改函数
46 
47 alter function fun_append(@args nvarchar(1024), @args2 nvarchar(1024))
48     returns nvarchar(1024)
49 as
50     begin
51         declare @result varchar(1024);    
52         --coalesce返回第一个不为null的值    
53         set @args = coalesce(@args, '');
54         set @args2 = coalesce(@args2, '');;
55         set @result = @args + @args2;
56         return @result;
57     end
58 go
59  
60 select dbo.fun_append(name, '#abc') from student;
61  
62 
63 # 返回table类型函数
64 
65 --返回table对象函数
66 select name, object_id, type from sys.objects where type in ('fn', 'if', 'tf') or type like '%f%';
67  
68 if (exists (select * from sys.objects where type in ('fn', 'if', 'tf') and name = 'fun_find_stuRecord'))
69     drop function fun_find_stuRecord
70 go
71 create function fun_find_stuRecord(@id int)
72     returns table
73 as
74     return (select * from student where id = @id);
75 go
76  
77 select * from dbo.fun_find_stuRecord(2);

 

转载于:https://www.cnblogs.com/chaoguoguo/articles/7399418.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值