MSSQLServer基础04(常用函数)
类型转换函数
CAST ( expression AS data_type)
CONVERT ( data_type, expression,[style])
对日期的转换。转换成各种国家格式的日期。
select convert(varchar(20),getdate(),104)
Style的格式,查sql帮助。(输入convert函数查询)
将日期转换为指定格式的字符串。日期→字符串
select
isnull(convert(varchar(10),tEnglish),'缺考')
from TblScore
select '平均成绩是' + cast(30 as varchar(3))
select cast(9.85 as int) ----------------舍去小数
ROUND() -------------------4舍5入
在SQL语句中,两个连续的 单引号 ,表示 一个单引号 。(单引号的转义符。)
字符串函数(*)
LEN() :计算字符串长度(字符的个数。)
datalength();//计算字符串所占用的字节数,不属于字符串函数。
测试varchar变量与nvarchar变量存储字符串a的区别。见备注1.
LOWER() 、UPPER () :转小写、大写
LTRIM():字符串左侧的空格去掉
RTRIM () :字符串右侧的空格去掉
LTRIM(RTRIM(' bb '))
LEFT()、RIGHT() 截取取字符串
SELECT LEFT('abcdefg',2)
SUBSTRING(string,start_position,length),索引从1开始。
参数string为主字符串,start_position为子字符串在主字符串中的起始位置,length为子字符串的最大长度。SELECT SUBSTRING('abcdef111',2,3)
日期函数
GETDATE() :取得当前日期时间
DATEADD (datepart , number, date ),计算增加以后的日期。参数date为待计算的日期;参数number为增量;参数datepart为计量单位,可选值见备注。DATEADD(DAY, 3,date)为计算日期date的3天后的日期,而DATEADD(MONTH ,-8,date)为计算日期date的8个月之前的日期 。
DATEDIFF ( datepart , startdate , enddate ) :计算两个日期之间的差额。 datepart 为计量单位,可取值参考DateAdd。
统计不同入学年数的学生个数:
select DateDiff(year,sInDate,getdate()),count(*) from student Group by DateDiff(year,sInDate,getdate())
DATEPART (datepart,date):返回一个日期的特定部分
========================================================================================================================
练习
创建一张表,记录电话呼叫员的工作流水,记录呼叫员编号、对方号码、通话开始时间、通话结束时间。建表、插数据等最后都自己写SQL语句。
要求:
输出所有数据中通话时间最长的5条记录。orderby datediff
输出所有数据中拨打长途号码(对方号码以0开头)的总时长。like、sum
输出本月通话总时长最多的前三个呼叫员的编号。
输出本月拨打电话次数最多的前三个呼叫员的编号.group by,count(*)
CREATE TABLE [CallRecords]
(
[Id] [int] NOT NULL identity(1,1),
[CallerNumber] [nvarchar](50), --三位数字,呼叫中心员工编号(工号)
[TelNum] [varchar](50),
[StartDateTime] [datetime] NULL,
[EndDateTime] [datetime] NULL --结束时间要大于开始时间,默认当前时间
)
--主键约束
alter table [CallRecords]
add constraint PK_CallRecords primary key(id)
--检查约束
alter table [CallRecords]
add constraint CK_CallRecords check(CallerNumber like ‘[0-9][0-9][0-9]’) \d{3}错误!!
alter table [CallRecords]
add constraint CK_CallRecords_EndDateTime check(EndDateTime > StartDateTime)
--默认约束
alter table [CallRecords]
add constraint DF_CallRecords default(getdate()) for EndDateTime
INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('001', '0208888888', CAST(0x00009DAF00A4CB80 AS DateTime), CAST(0x00009DAF00A62E94 AS DateTime));
INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('001', '0208888888', CAST(0x00009DB000D63BC0 AS DateTime), CAST(0x00009DB000D68DC8 AS DateTime));
INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('001', '89898989', CAST(0x00009DB000E85C60 AS DateTime), CAST(0x00009DB000E92F50 AS DateTime));
INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('002', '98987676', CAST(0x00009DB2015BB7A0 AS DateTime), CAST(0x00009DB2015C4DA0 AS DateTime));
INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('002', '02188839389', CAST(0x00009DA4014C9C70 AS DateTime), CAST(0x00009DA4014E0308 AS DateTime));
INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('001', '767676766', CAST(0x00009DB400DAA0C0 AS DateTime), CAST(0x00009DB400DD5FE0 AS DateTime));
INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('003', '0227864656', CAST(0x00009DB200B9AB40 AS DateTime), CAST(0x00009DB200B9FC1C AS DateTime));
INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('003', '676765777', CAST(0x00009DB8014042B8 AS DateTime), CAST(0x00009DB80141804C AS DateTime));
INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('001', '89977653', CAST(0x00009D9A00FB9898 AS DateTime), CAST(0x00009D9A00FE6118 AS DateTime));
INSERT [dbo].[CallRecords] ([CallerNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('004', '400400400', CAST(0x00009D9A00FB9898 AS DateTime), CAST(0x00009D9A00FE6118 AS DateTime));
--查询通话时间最长的条记录
select datediff(second,StartDateTime,EndDateTime) from CallRecords
select top 5 datediff(second,StartDateTime,EndDateTime),Id, CallerNumber, TelNum, StartDateTime, EndDateTime
from CallRecords order by datediff(second,StartDateTime,EndDateTime) desc
--查询长途的通话总时长
select sum(datediff(second,StartDateTime,EndDateTime)) from CallRecords
where TelNum like '0%'
--查询本月通话总时长最多的前三个呼叫员的编号
select top 3 [CallerNumber],sum(datediff(ss,[StartDateTime],[EndDateTime])) from CallRecords
--where year(StartDateTime) = year(getdate()) and month(StartDateTime)= month(getdate())
where datediff(month,[StartDateTime],‘2010-07-1’) = 0 –判断本月的另一种方法。
group by [CallerNumber]
order by sum(datediff(ss,[StartDateTime],[EndDateTime])) desc
--查询本月拨打电话次数最多的前三个呼叫员的编号
select top 3 [CallerNumber],count(*) from CallRecords
where datediff(month,[StartDateTae],'2010-07-1') = 0
group by [CallerNumber]
order by count(*) desc
MSSQLServer基础04(常用函数)的更多相关文章
- MySQL基础之常用函数
数学函数的使用 常用数学函数 函数 作用 函数 作用 ceil() 进一取整 abs() 取绝对值 floor() 舍掉小数部分 power() 幂运算 round() 四舍五入 pi() 圆周率 t ...
- Python基础:常用函数
1:enumerate enumerate(sequence, start=0) 该函数返回一个enumerate对象(一个迭代器).其中的sequence参数可以是序列.迭代器或者支持迭代的其他对象 ...
- VBS基础篇 - 常用函数
Option Explicit '*********************************Date/Time函数******************************* 'CDate函 ...
- Python基础(一)常用函数
1.map() 此函数可以,将列表内每一个元素进行操作,并返回列表 原型 map(function,[list]) def fc(x): return x * 2 print(map(fc,[1,2, ...
- Greenplum入门——基础知识、安装、常用函数
Greenplum入门——基础知识.安装.常用函数 2017年10月08日 22:03:09 在咖啡里溺水的鱼 阅读数:8709 版权声明:本文为博主原创,允许非商业性质转载但请注明原作者和出处 ...
- java基础--常用函数总结
java基础--常用函数总结 2019-3-16-23:28:01-----云林原创 1.split()字符串分割函数 将一个字符串分割为子字符串,然后将结果作为字符串数组返回. 2.Math.flo ...
- MySQL基础篇(3)常用函数和运算符
一.字符串函数(索引位置都从1开始) CONCAT(S1,S2,...Sn): 连接S1,S2,...Sn为一个字符串,任何字符串与NULL进行连接的结果都是NULL INSERT(str,x,y,i ...
- SQL基础随记1 SQL分类 常用函数 ALL ANY EXISTS IN 约束
SQL基础随记1 SQL分类 常用函数 ALL ANY EXISTS IN 约束 其实这里知识不难,只是好久不接触突然被问的话有时还真的一时答不上,自己写一遍胜过盲扫.当然,也有些常读常新的地方会 ...
- SQL操作数据——SQL组成,查询基础语法,where,Oracle常用函数等
SQL组成 DML数据操作语言 DCL数据控制语言 DQL数据查询语言 DDL数据定义语言 查询基础语法 记录筛选 where 子句 记录筛选 where 子句 实例练习 实例练习 Select语句中 ...
随机推荐
- C++ typedef用法小结
一.typedef的四个用法 用法一: 为复杂的声明定义一个新的简单的别名.方法是:在原来的声明里逐步用别名替换一部分复杂声明,如此循环,把带变量名的部分留到最后替换,得到的就是原声明的最简化版.举例 ...
- .Net Core 项目中的包引用探索(使用VSCode)
本文组织有点乱,先说结论吧: 1 在 project.json 文件中声明包引用. 而不是像以前那样可以直接引用 dll. 2 使用 dotnet restore 命令后,nuget 会把声明的依赖项 ...
- OCI的结果输出
绑定变量,把结果以列的方式输出到每一字段输出到一个数组里
- C/C++ union
叙述原因: union data{ int a;double b;}; 对于union,实际中用的并不多,之前也知道怎样计算union的单元(在字对齐的基础上取最大成员所占的内存大小),比如 unio ...
- pthread 实现生产者消费者问题
经典的生产者消费者问题,在这里用信号量和互斥量来实现生产和消费者模型 #include<cstdlib> #include<cstdio> #include<unis ...
- jquery + json 操作
jquery 读取集合对象多是要与json进行解析操作的,以下自己经过多方资料查找,终于有一套自己的方式组合. 1.首先创建web services或一般处理程序,用于显示获取Datatable对象 ...
- =====关于swing的一些收集-swing大收集======
一篇经典的 介绍netbeans中swing 应用程序框架的文章 http://blog.csdn.net/tangwing/article/details/5745075 Swing外观框架 Bea ...
- flex 弹性盒子模型一些案例.html
Flexbox是布局模块,而不是一个简单的属性,它包含父元素和子元素的属性. Flex元素是可以让你的布局根据浏览器的大小变化进行自动伸缩. 自适应导航 <ul class="navi ...
- spring-cloud-hystrix熔断
依赖pom <dependencyManagement> <dependencies> <dependency> <groupId>org.spring ...
- MYSQL数据库主主同步实战
MYSQL支持单向.异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器.主服务器将更新写入二进制日志文件,并维护日志文件的一个索引以跟踪日志循环.当一个从服务器连接到主服务 ...