1. 当前系统日期、时间
select getdate() 
2. dateadd 在向指定日期加上一段时间的基础上,返回新的 datetime 值
例如:向日期加上2天
select dateadd(day,2,'2004-10-15') --返回:2004-10-17 00:00:00.000

3. datediff 返回跨两个指定日期的日期和时间边界数。
select datediff(day,'2004-09-01','2004-09-18') --返回:17

4. datepart 返回代表指定日期的指定日期部分的整数。
SELECT DATEPART(month, '2004-10-15') --返回 10

5. datename 返回代表指定日期的指定日期部分的字符串
SELECT datename(weekday, '2004-10-15') --返回:星期五

6. day(), month(),year() --可以与datepart对照一下

select 当前日期=convert(varchar(10),getdate(),120)
,当前时间=convert(varchar(8),getdate(),114)

7. select datename(dw,'2004-10-15')

select 本年第多少周=datename(week,getdate())
,今天是周几=datename(weekday,getdate())

函数 参数/功能 
GetDate( ) --返回系统目前的日期与时间 
DateDiff (interval,date1,date2) --以interval 指定的方式,返回date2 与date1两个日期之间的差值 date2-date1
DateAdd (interval,number,date) --以interval指定的方式,加上number之后的日期 
DatePart (interval,date) ---返回日期date中,interval指定部分所对应的整数值 
DateName (interval,date) --返回日期date中,interval指定部分所对应的字符串名称

参数 interval的设定值如下:

值 缩 写(Sql Server) Access 和 ASP 说明 
Year Yy yyyy 年 1753 ~ 9999 
Quarter Qq q 季 1 ~ 4 
Month Mm m 月1 ~ 12 
Day of year Dy y 一年的日数,一年中的第几日 1-366 
Day Dd d 日,1-31 
Weekday Dw w 一周的日数,一周中的第几日 1-7 
Week Wk ww 周,一年中的第几周 0 ~ 51 
Hour Hh h 时0 ~ 23 
Minute Mi n 分钟0 ~ 59 
Second Ss s 秒 0 ~ 59 
Millisecond Ms - 毫秒 0 ~ 999

access 和 asp 中用date()和now()取得系统日期时间;其中DateDiff,DateAdd,DatePart也同是能用于Access和asp中,这些函数的用法也类似

举例:
1.GetDate() 用于sql server :select GetDate()

2.DateDiff('s','2005-07-20','2005-7-25 22:56:32')返回值为 514592 秒
DateDiff('d','2005-07-20','2005-7-25 22:56:32')返回值为 5 天

3.DatePart('w','2005-7-25 22:56:32')返回值为 2 即星期一(周日为1,周六为7)
DatePart('d','2005-7-25 22:56:32')返回值为 25即25号
DatePart('y','2005-7-25 22:56:32')返回值为 206即这一年中第206天
DatePart('yyyy','2005-7-25 22:56:32')返回值为 2005即2005年

SQL Server DATEPART() 函数返回 SQLServer datetime 字段的一部分。

SQL Server DATEPART() 函数的语法是: 
DATEPART(portion, datetime)

其中 datetime 是 SQLServer datetime 字段和部分的名称是下列之一: Ms for Milliseconds
Yy for Year
Qq for Quarter of the Year
Mm for Month
Dy for the Day of the Year
Dd for Day of the Month
Wk for Week
Dw for the Day of the Week
Hh for Hour
Mi for Minute
Ss for Second

--1.编写函数,实现按照'年月日,星期几,上午下午晚上'输出时间信息(2009年3月16日星期一下午)
select datename(yy,getdate()) + '年' +
datename(mm,getdate()) + '月' + 
datename(dd,getdate()) + '日' +
datename(weekday,getdate()) +
case when datename(hh,getdate()) < 12 then '上午' else '下午' end 
--2.编写函数,根据输入时间。输出该天是该年的第几天
select datepart(dy,getdate())
--3.求出随机输出字符‘a-z
select char(97+abs(checksum(newid()))%26)
select char(97+rand()*26)

【转】sql server日期比较的更多相关文章

  1. sql server日期时间转字符串

    一.sql server日期时间函数Sql Server中的日期与时间函数 1.  当前系统日期.时间     select getdate()  2. dateadd  在向指定日期加上一段时间的基 ...

  2. sql server 日期

    在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...

  3. SQL Server日期时间格式转换字符串详解 (详询请加qq:2085920154)

    在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...

  4. SQL Server日期时间格式转换字符串

    在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...

  5. SQL Server日期与字符串之间的转换

    本文导读:在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.下面主要就介绍一下SQL Serv ...

  6. SQL Server日期和时间的格式

    在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...

  7. SQL Server 日期的加减函数: DATEDIFF DATEADD

    SQL Server 日期的加减函数: DATEDIFF    DATEADD DATEDIFF: 返回跨两个指定日期的日期边界数和时间边界数, 语法:DATEDIFF ( datepart , st ...

  8. SQL Server日期函数总结

    获得一个月的天数:首先到得一个月最后一天的日期,通过 SQL Server 日期函数 day() 取得日期中的“天 ”部分 获得 2008 年 2 月份的天数:select day(cast('200 ...

  9. sql server日期时间转字符串(转)

    一.sql server日期时间函数Sql Server中的日期与时间函数 1.  当前系统日期.时间     select getdate()  2. dateadd  在向指定日期加上一段时间的基 ...

  10. SQL Server日期时间格式转换字符串详解

    本文我们主要介绍了SQL Server日期时间格式转换字符串的相关知识,并给出了大量实例对其各个参数进行对比说明,希望能够对您有所帮助. 在SQL Server数据库中,SQL Server日期时间格 ...

随机推荐

  1. Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad

    Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad 方法一:安装单独组件 ...

  2. Python廖雪峰学习笔记——单元测试

    定义:对一个模块.一个类.一个函数进行进行正确性检验的测试性工作.当我们对函数或者模块等进行修改时,单元测试就显得尤为重要. 单元测试 = 测试用例(用来测试的数据)+测试模块

  3. PHPhotos

    PHPhotoLibrary: @abstract A PHPhotoLibrary provides access to the metadata and image data for the ph ...

  4. Mutual Training for Wannafly Union #6 E - Summer Trip(并查集)

    题目链接:http://www.spoj.com/problems/IAPCR2F/en/ 题目大意: 给m个数字代表的大小,之后n组数据,两两关联,关联后的所有数字为一组,从小到大输出组数以及对应的 ...

  5. (6)Oracle基础--简单查询

    .基本查询语句  SELECT [DISTINCT] column_name1,... | * FROM table_name [WHERE conditions]; P: DISTINCT关键字的作 ...

  6. windows 安装nexus3

    下载地址 nexus官网下载页面 文件名:nexus-3.3.1-01-win64.zip,解压,cd到bin目录 运行: nexus.exe /run 1 安装成系统服务: nexus.exe /i ...

  7. [JavaScript] css将footer置于页面最底部

    <!-- 父层 --> <div id="wapper"> <!-- 主要内容 --> <div id="main-conten ...

  8. 在vue项目中安装使用Mint-UI

    一.Mint UI 是 由饿了么前端团队推出的 一个基于 Vue.js 的移动端组件库,具有以下特性:       使用文档: http://mint-ui.github.io/#!/zh-cn Mi ...

  9. Linux CentOS7系统中ssh的用法

    大家都知道,公司买上服务器,不可能实时在线操作虚拟机,也没有那个时间和精力登录到公司的云服务商官网进行操作,一来不安全,二来也效率不高. 如果是购买的虚拟主机,你可以使用ftp进行本地程序文件传输和从 ...

  10. Linux的管道命令

    Linux的管道命令 管道命令(Pipe) 管道命令用"|"来表示,管道命令需要接收前一个命令的输出来进行操作,但不能处理前一个命令的错误. //选取界面:cut,grep cut ...