类型转换函数

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

最新文章

  1. iOS 陀螺仪,加速度计
  2. Laravel5.0学习--01 入门
  3. C# List.ForEach 方法
  4. 多播程序设计(基于UDP协议)
  5. 运用Fluxion高效破解WiFi密码
  6. 使用HttpsURLConnection发送POST请求
  7. 对WPF中MeasureOverride 和ArrangeOverride 浅理解
  8. 如何使用robots不让百度和google收录
  9. ERP实施员的保密要求
  10. 依赖layui form模块 复选框tree插件(拓展可根据属性单选还是多选,数据反选)
  11. 剑指Offer——归并排序思想应用
  12. JAVA 第1课
  13. React Router 4.0 ---- 嵌套路由和动态路由
  14. 【Socket】Java Socket编程基础及深入讲解
  15. pytorch torchvision对图像进行变换
  16. COCO数据集格式互换
  17. 阿里巴巴 Java 代码规范
  18. (ubuntu ufw)My firewall is blocking network connections from the docker container to outside
  19. cudnn 安装
  20. USACO08MAR土地购买 与 APIO2010特别行动队

热门文章

  1. 119. Pascal's Triangle II
  2. 《大话设计模式》学习笔记0:设计模式的原则 && UML类图
  3. 使用UDP进行数据发送的实例一
  4. [转载]mysql慢日志文件分析处理
  5. TweenMax动画库学习(五)
  6. 【原】在一般处理程序中设置session
  7. Mysql MyISAM数据库批量转换表引擎为Innodb
  8. vhost设定
  9. ics OverbyteIcsHttpProt
  10. 以a为变量名,给出下列描述的定义