类型转换函数

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(常用函数)的更多相关文章

  1. MySQL基础之常用函数

    数学函数的使用 常用数学函数 函数 作用 函数 作用 ceil() 进一取整 abs() 取绝对值 floor() 舍掉小数部分 power() 幂运算 round() 四舍五入 pi() 圆周率 t ...

  2. Python基础:常用函数

    1:enumerate enumerate(sequence, start=0) 该函数返回一个enumerate对象(一个迭代器).其中的sequence参数可以是序列.迭代器或者支持迭代的其他对象 ...

  3. VBS基础篇 - 常用函数

    Option Explicit '*********************************Date/Time函数******************************* 'CDate函 ...

  4. Python基础(一)常用函数

    1.map() 此函数可以,将列表内每一个元素进行操作,并返回列表 原型 map(function,[list]) def fc(x): return x * 2 print(map(fc,[1,2, ...

  5. Greenplum入门——基础知识、安装、常用函数

    Greenplum入门——基础知识.安装.常用函数 2017年10月08日 22:03:09 在咖啡里溺水的鱼 阅读数:8709    版权声明:本文为博主原创,允许非商业性质转载但请注明原作者和出处 ...

  6. java基础--常用函数总结

    java基础--常用函数总结 2019-3-16-23:28:01-----云林原创 1.split()字符串分割函数 将一个字符串分割为子字符串,然后将结果作为字符串数组返回. 2.Math.flo ...

  7. MySQL基础篇(3)常用函数和运算符

    一.字符串函数(索引位置都从1开始) CONCAT(S1,S2,...Sn): 连接S1,S2,...Sn为一个字符串,任何字符串与NULL进行连接的结果都是NULL INSERT(str,x,y,i ...

  8. SQL基础随记1 SQL分类 常用函数 ALL ANY EXISTS IN 约束

    SQL基础随记1 SQL分类 常用函数 ALL ANY EXISTS IN 约束   其实这里知识不难,只是好久不接触突然被问的话有时还真的一时答不上,自己写一遍胜过盲扫.当然,也有些常读常新的地方会 ...

  9. SQL操作数据——SQL组成,查询基础语法,where,Oracle常用函数等

    SQL组成 DML数据操作语言 DCL数据控制语言 DQL数据查询语言 DDL数据定义语言 查询基础语法 记录筛选 where 子句 记录筛选 where 子句 实例练习 实例练习 Select语句中 ...

随机推荐

  1. jQuery中的getter和setter方法

    1.attr()方法是jQuery中用于HTML属性的getter/setter.一个相关函数是removeAttr(). 2.css()方法和attr()方法很类似,只是css()方法作用于元素的c ...

  2. 编辑器笔记——sublime text3 编译sass

    如已经安装sass 和 sass build两个插件到ST,command+b编译也没有问题,那么另外安装koala,在koala中引入你要编辑的sass,并把该sass文件设置为自动编译,那么用ST ...

  3. Contest1065 - 第四届“图灵杯”NEUQ-ACM程序设计竞赛(个人赛)G爬楼梯

    题目描述 由于第m个台阶上有好吃的薯条,所以薯片现在要爬一段m阶的楼梯. 薯片每步最多能爬k个阶梯,但是每到了第i个台阶,薯片身上的糖果都会掉落ai个,现在问你薯片至少得掉多少糖果才能得到薯条? 输入 ...

  4. 如何找到Linux下常用命令的源码

    Linux系统,常用命令的来源很多,有些命令是shell自带的,比如cd,通过执行help命令,可以查看当前系统所有的内置命令. 用type <cmd_name>来查看一个命令是否为内置命 ...

  5. mysql 恢复备份时出错 Unknown command ‘\”

    今天恢复mysql备份时,出现了Unknown command ‘\”这样的错误,原以为是备份文件出错,重新备份导入时还是出错.研究了一凡是因为编码的问题,解决方案如下: 使用你导出备份时的编码重新导 ...

  6. c#winform,制作可编辑html编辑器

    大神勿喷,新手记笔记 材料 网上下载kindeditor,动手在写个htmldome,图中的e.html.然后全部扔到了bin/debug下面,(x86是要扔到bin/x86/debug) 中间bod ...

  7. Android Handler 原理

    在android中提供了一种异步回调机制Handler,使用它,我们可以在完成一个很长时间的任务后做出相应的通知 handler基本使用: 在主线程中,使用handler很简单,new一个Handle ...

  8. 定时备份服务器数据库(借助windows任务计划以及mysqldump)

    最近社区多了,考虑到数据的安全性,要每天备份一次数据库,以防万一: linux目前还不是很了解,先用windows的计划任务吧: 大体思路就是 借用windows的计划任务来执行备份远程数据库到本地: ...

  9. Gazebo Ros入门

    教程代码 First step with gazebo and ros • setup a ROS workspace • create projects for your simulated rob ...

  10. .NET平台一些概念

    1.什么是CLR CLR(Common Language Runtime)公共语言远行时,是一个可由多种编程语言使用的“远行时”.CLR的核心功能(比如内存管理.程序集加载.安全性.异常处理和线程同步 ...