SQL时间第一期_获取系统年月日时分秒
select GETDATE() as '当前日期',
DateName(year,GetDate()) as '年',
DateName(month,GetDate()) as '月',
DateName(day,GetDate()) as '日',
DateName(dw,GetDate()) as '星期',
DateName(week,GetDate()) as '周数',
DateName(hour,GetDate()) as '时',
DateName(minute,GetDate()) as '分',
DateName(second,GetDate()) as '秒'
结果:
2009-08-13 23:07:15.403 2009 08 13 星期四 33 23 7 15
2007-11-13
sql server2000 时间操作
1.显示本月第一天
SELECT DATEADD(mm,DATEDIFF(mm,0,getdate()),0)
select convert(datetime,convert(varchar(8),getdate(),120)+'01',120)
2.显示本月最后一天
select dateadd(day,-1,convert(datetime,convert(varchar(8),dateadd(month,1,getdate()),120)+'01',120))
SELECT dateadd(ms,-3,DATEADD(mm,DATEDIFF(m,0,getdate())+1,0))
3.上个月的最后一天
SELECT dateadd(ms,-3,DATEADD(mm,DATEDIFF(mm,0,getdate()),0))
4.本月的第一个星期一
select DATEADD(wk,DATEDIFF(wk,0, dateadd(dd,6-datepart(day,getdate()),getdate())),0)
5.本年的第一天
SELECT DATEADD(yy,DATEDIFF(yy,0,getdate()),0)
6.本年的最后一天
SELECT dateadd(ms,-3,DATEADD(yy,DATEDIFF(yy,0,getdate())+1,0))
7.去年的最后一天
SELECT dateadd(ms,-3,DATEADD(yy,DATEDIFF(yy,0,getdate()),0))
8.本季度的第一天
SELECT DATEADD(qq,DATEDIFF(qq,0,getdate()),0)
9.本周的星期一
SELECT DATEADD(wk,DATEDIFF(wk,0,getdate()),0)
10.查询本月的记录
select * from tableName where DATEPART(mm, theDate) = DATEPART(mm, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
11.查询本周的记录
select * from tableName where DATEPART(wk, theDate) = DATEPART(wk, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
12.查询本季的记录
select * from tableName where DATEPART(qq, theDate) = DATEPART(qq, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())
其中:GETDATE()是获得系统时间的函数。
13.获取当月总天数:
select DATEDIFF(dd,getdate(),DATEADD(mm, 1, getdate()))
select datediff(day,
dateadd(mm, datediff(mm,'',getdate()), ''),
dateadd(mm, datediff(mm,'',getdate()), '1900-02-01'))
14.获取当前为星期几
DATENAME(weekday, getdate())
15. 当前系统日期、时间
select getdate()
16. dateadd 在向指定日期加上一段时间的基础上,返回新的 datetime 值
例如:向日期加上2天
select dateadd(day,2,'2004-10-15') --返回:2004-10-17 00:00:00.000
17. datediff 返回跨两个指定日期的日期和时间边界数。
select datediff(day,'2004-09-01','2004-09-18') --返回:17
18. datepart 返回代表指定日期的指定日期部分的整数。
SELECT DATEPART(month, '2004-10-15') --返回 10
年为year,月为month,日为day,小时hour,分为minute,秒为second
19. datename 返回代表指定日期的指定日期部分的字符串
SELECT datename(weekday, '2004-10-15') --返回:星期五
17. day(), month(),year() --可以与datepart对照一下
select 当前日期=convert(varchar(10),getdate(),120),当前时间=convert(varchar(8),getdate(),114)
select datename(dw,'2004-10-15')
select 本年第多少周=datename(week,'2004-10-15'),今天是周几=datename(weekday,'2004-10-15')
函数 参数/功能
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) 说明
Year Yy 年 1753 ~ 9999
Quarter Qq 季 1 ~ 4
Month Mm 月1 ~ 12
Day of year Dy 一年的日数,一年中的第几日 1-366
Day Dd 日,1-31
Weekday Dw 一周的日数,一周中的第几日 1-7
Week Wk 周,一年中的第几周 0 ~ 51
Hour Hh 时0 ~ 23
Minute Mi 分钟0 ~ 59
Second Ss 秒 0 ~ 59
Millisecond Ms 毫秒 0 ~ 999
举例:
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年
应用示例:
查询某个日期之间的记录数据:
select * from 表 where 开始时间>'2005-02-01' and 结束时间<='2005-06-05'order by id desc
查询最近30内的记录数据:
select * from 表 where datediff(Dd,last_date,getdate())<=30 order by id desc
查询最近一周内的点击率大于100的记录数据:
select * from t_business_product where hit_count>100 and datediff(Dw,last_date,getdate())<=7 order by id desc
查询某一年(如2006年)的记录数据:
select * from 表 where DatePart(Yy,last_date)=2006 order by id desc
或
select * from 表 where DatePart(Year,last_date)=2006 order by id desc
如查询系统当前年份插入的一年内的数据:
select * from 表 where DatePart(Yy,getdate())=DatePart(Yy,getdate()) order by id desc
取系统日期 并将 日期 分开
select 当前日期=convert(varchar(10),dateadd(day,-1,getdate()),120),当前时间=convert(varchar(8),getdate(),114)
---------------------取 年月日
year(),month(),date()
SQL时间第一期_获取系统年月日时分秒的更多相关文章
- C#计算两个时间的时间差,精确到年月日时分秒
喏,计算两个时间的时间差,精确到年月日时分秒 看起来比较笨的方法了,不知道有没有改进 DateTime d1 = new DateTime(2016, 4, 1, 0, 0, 0); DateTime ...
- JavaScript中的内置对象-8--4.date对象中-获取,设置日期时间的方法; 获取,设置年月日时分秒及星期的方法;
学习目标 1.掌握创建日期对象的方法 2.掌握date对象中获取日期时间的方法 3.掌握date对象中设置日期时间的方法 如何创建一个日期对象 语法:new Date(); 功能:创建一个日期时间对象 ...
- sql server获取当前年月日 时分秒
获取当前年月日(字符串): ),) 获取当前时间的时分秒(':'隔开): ),) 将年月日时分秒拼接成一条字符串: ),)),),':','')
- SQL时间第二期_时间格式化
0 或 100 (*) 默认值 mon dd yyyy hh:miAM(或 PM) 1 101 美国 mm/dd/yyyy ...
- 时间转换(字符串转date 年月日时分秒 格式)
/** * 时间转换 * @param data * @return */ public String getValidDateStr(Date data) { ...
- Python datetime获取当前年月日时分秒
from datetime import datetime now_time = datetime.now() str_time = now_time.strftime("%Y-%m-%d ...
- js 获取当前年月日时分秒星期
$("#aa").click(function () { var date = new Date(); this.year = date.getFullYear(); this.m ...
- C语言 - 获取系统时间 以年月日时分秒的形式输出
ESP32需要给下位机通过UART发送时间戳,形式是年月日时分秒的十六进制数据包. #include <stdio.h> #include <time.h> int main( ...
- Sql 中获取年月日时分秒的函数
getdate():获取系统当前时间 dateadd(datepart,number,date):计算在一个时间的基础上增加一个时间后的新时间值,比如:dateadd(yy,30,getdate()) ...
随机推荐
- [转]SQL语句优化技术分析
一.操作符优化 1.IN 操作符 用IN写出来的SQL的优点是比较容易写及清晰易懂,这比较适合现代软件开发的风格.但是用IN的SQL性能总是比较低的,从Oracle执行的步骤来分析用IN的SQL与不用 ...
- 第二篇 顾问实施ERP与医生看病过程类比
我从08年开始涉足企业信息化咨询行业.当时做的第一个项目是汕头某黄金珠宝公司的SAP ERP业务优化项目.我在项目上担任顾问助理的角色.我们公司的老板据说是以前ORACLE公司华南区的总监,后来是格兰 ...
- Fragment学习(一)
Fragment界面添加 了解过fragment的生命周期等简单知识,于是去看官方文档来了解更多相关内容,要添加fragment到我们的UI界面中,给出了两种常用的方法,第一个是在activity的布 ...
- JSON 之 SuperObject(10): Merge、Clone、ForcePath
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- phpStorm连接mysql
小结:牛逼的IDE
- LA 4794 Sharing Chocolate
大白书中的题感觉一般都比较难,能理解书上代码就已经很不错了 按照经验,一般数据较小的题目,都有可能是用状态压缩来解决的 题意:问一个面积为x×y的巧克力,能否切若干刀,将其切成n块面积为A1,A2,, ...
- Servlet生命周期以及获取参数
1. 创建Servlet几种方式 1) 实现Servlet接口 控制Servlet的生命周期 构造器 init() service() des ...
- impersonate a user
// This sample demonstrates the use of the WindowsIdentity class to impersonate a user. // IMPORTANT ...
- Bootstrap-select:美化原生select
官网:http://silviomoreto.github.io/bootstrap-select/ 1.下载zip 2.html代码 <select class="selectpic ...
- 参考:iPhone OS 3.0中的字体列表
字体是我们在iPhone开发中经常需要用到的.但是iPhone里面到底内置了哪些字体呢?下面就是一个常用的列表. Family name: AppleGothic Font name: AppleGo ...