设为首页 收藏本站
查看: 1003|回复: 0

[经验分享] Sql Server起今为止最全的【日期函数大全】!(超经典,全部为示例)

[复制链接]

尚未签到

发表于 2016-11-10 08:17:26 | 显示全部楼层 |阅读模式
select convert(varchar(10),getdate(),120)  -- 只返回当前日期,且为2012-12-12格式(最有用)
        datediff(day,createdate,GetDate())=0      -- 判断是否当天,createdate为日期字段
--                                                                                    ╔════════════════════╗
-- =================================================================================  ║    第一天、第几月  ║
--                                                                                    ╚════════════════════╝
-- 1.一个月第一天的
Select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)
-- 2.本周的星期一
Select DATEADD(wk, DATEDIFF(wk,0,getdate()), 0)
-- 3.一年的第一天
Select DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
-- 4.季度的第一天
Select DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)
-- 5.当天的半夜
Select DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)
-- 6.上个月的最后一天
Select dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0))
-- 7.去年的最后一天
Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0))
-- 8.本月的最后一天
Select dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))
-- 9.本年的最后一天
Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate())+1, 0))
-- 10.本月的第一个星期一
select DATEADD(wk, DATEDIFF(wk,0,dateadd(dd,6-datepart(day,getdate()),getdate())), 0)
select 本年第多少周=datename(week,getdate())
,今天是周几=datename(weekday,getdate())
--  dateadd 在向指定日期加上一段时间的基础上,返回新的datetime值
-- 向日期加上2天 或 增加1个月
select dateadd(day,2,'2004-10-15') --返回:2004-10-17 00:00:00.000
select dateadd(month,2,'2004-10-15') --返回:2004-12-17 00:00:00.000
--3. datediff 返回跨两个指定日期的日期和时间边界数。
select datediff(day,'2004-09-01','2004-09-18') --返回天数:17
select DateDiff(s,'2005-07-20','2005-7-25 22:56:32') --返回值为 514592 秒
select DateDiff(ms,'2005-07-20','2005-7-25 22:56:32') --返回值为 微秒
select DateDiff(d,'2005-07-20','2005-7-25 22:56:32') -- 返回值为 5 天
select DatePart(w,'2005-7-25 22:56:32')--返回值为 2 即星期一(周日为1,周六为7)
select DatePart('d','2005-7-25 22:56:32')--返回值为 25即25号
select DatePart('y','2005-7-25 22:56:32')--返回值为 206即这一年中第206天
select DatePart('yyyy','2005-7-25 22:56:32')--返回值为 2005即2005年
--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指定部分所对应的字符串名称

--                                                                                    ╔════════════════════╗
-- =================================================================================  ║  当前时间函数      ║
--                                                                                    ╚════════════════════╝
-- 返回当前日期和时间
select GETDATE()
--  返回代表指定日期的指定日期部分的整数。
select datepart(month, '2004-10-15') --返回 月
select datepart(day, '2004-10-15') --返回 日
select datepart(year, getdate()) --返回 年
select convert(varchar(8),getdate(),114)  -- 当前时间
select datename(weekday, getdate()) --返回:星期五
select datepart(weekday, getdate()) --返回:小写星期2-1
select convert(varchar(10),getdate(),120)  -- 当前日期
select datepart(S, '2004-10-15') --返回 月
--  返回时间到豪秒
Select CONVERT(VARCHAR(30),GETDATE(),9)
--  获取当前日期,年、月、日、周、时、分、秒
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 '秒'
print DateName(second,GetDate())+'1'



--  格式
select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','')
20040912110608
select CONVERT(varchar(12) , getdate(), 111 )
2004/09/12
select CONVERT(varchar(12) , getdate(), 112 )
20040912
select CONVERT(varchar(12) , getdate(), 102 )
2004.09.12
select CONVERT(varchar(12) , getdate(), 101 )
09/12/2004
select CONVERT(varchar(12) , getdate(), 103 )
12/09/2004
select CONVERT(varchar(12) , getdate(), 104 )
12.09.2004
select CONVERT(varchar(12) , getdate(), 105 )
12-09-2004
select CONVERT(varchar(12) , getdate(), 106 )
12 09 2004
select CONVERT(varchar(12) , getdate(), 107 )
09 12, 2004
select CONVERT(varchar(12) , getdate(), 108 )
11:06:08
select CONVERT(varchar(12) , getdate(), 109 )
09 12 2004 1
select CONVERT(varchar(12) , getdate(), 110 )
09-12-2004
select CONVERT(varchar(12) , getdate(), 113 )
12 09 2004 1
select CONVERT(varchar(12) , getdate(), 114 )
11:06:08.177
--                                                                                    ╔════════════════════╗
-- =================================================================================  ║  数据库时间函数    ║
--                                                                                    ╚════════════════════╝
-- 查询最近一个月内的点击率大于100的记录数据:
select * from t_business_product where hit_count>100 and 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
-- 你可以使用LIKE来返回正确的记录。通过在日期表达式中包含通配符“%”,
-- 你可以匹配一个特定日期的所有时间。这里有一个例子:
--这个语句可以匹配正确的记录。因为通配符“%”代表了任何时间。
Select * FROM weblog Where entrydate LIKE ‘Dec 25 2000%’
--                                                                                    ╔════════════════════╗
-- =================================================================================  ║ CAST和CONVERT函数  ║
--                                                                                    ╚════════════════════╝
select @@version




select convert(varchar(10),getdate(),120) -- 只返回当前日期,且为2012-12-12格式(最有用)

--                                          ╔════════════════════╗
-- ================================================================================= ║  第一天、第几月 ║
--                                          ╚════════════════════╝


-- 1.一个月第一天的
Select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)


-- 2.本周的星期一
Select DATEADD(wk, DATEDIFF(wk,0,getdate()), 0)


-- 3.一年的第一天
Select DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)


-- 4.季度的第一天
Select DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)


-- 5.当天的半夜
Select DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)


-- 6.上个月的最后一天
Select dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0))


-- 7.去年的最后一天
Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0))


-- 8.本月的最后一天
Select dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))


-- 9.本年的最后一天
Select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate())+1, 0))


-- 10.本月的第一个星期一
select DATEADD(wk, DATEDIFF(wk,0,dateadd(dd,6-datepart(day,getdate()),getdate())), 0)


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



-- dateadd 在向指定日期加上一段时间的基础上,返回新的datetime值
-- 向日期加上2天 或 增加1个月
select dateadd(day,2,'2004-10-15') --返回:2004-10-17 00:00:00.000
select dateadd(month,2,'2004-10-15') --返回:2004-12-17 00:00:00.000


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

select DateDiff(s,'2005-07-20','2005-7-25 22:56:32') --返回值为 514592 秒

select DateDiff(ms,'2005-07-20','2005-7-25 22:56:32') --返回值为 微秒
select DateDiff(d,'2005-07-20','2005-7-25 22:56:32') -- 返回值为 5 天
select DatePart(w,'2005-7-25 22:56:32')--返回值为 2 即星期一(周日为1,周六为7)
select DatePart('d','2005-7-25 22:56:32')--返回值为 25即25号
select DatePart('y','2005-7-25 22:56:32')--返回值为 206即这一年中第206天
select DatePart('yyyy','2005-7-25 22:56:32')--返回值为 2005即2005年


--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指定部分所对应的字符串名称




--                                          ╔════════════════════╗
-- ================================================================================= ║ 当前时间函数   ║
--                                          ╚════════════════════╝
-- 返回当前日期和时间
   select GETDATE()


-- 返回代表指定日期的指定日期部分的整数。
select datepart(month, '2004-10-15') --返回 月
select datepart(day, '2004-10-15') --返回 日
select datepart(year, getdate()) --返回 年
select convert(varchar(8),getdate(),114) -- 当前时间
select datename(weekday, getdate()) --返回:星期五
select datepart(weekday, getdate()) --返回:小写星期2-1
select convert(varchar(10),getdate(),120) -- 当前日期
select datepart(S, '2004-10-15') --返回 月
-- 返回时间到豪秒

Select CONVERT(VARCHAR(30),GETDATE(),9)


-- 获取当前日期,年、月、日、周、时、分、秒
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 '秒'



print DateName(second,GetDate())+'1'















-- 格式


select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','')
20040912110608


select CONVERT(varchar(12) , getdate(), 111 )
2004/09/12


select CONVERT(varchar(12) , getdate(), 112 )
20040912


select CONVERT(varchar(12) , getdate(), 102 )
2004.09.12


select CONVERT(varchar(12) , getdate(), 101 )
09/12/2004


select CONVERT(varchar(12) , getdate(), 103 )
12/09/2004


select CONVERT(varchar(12) , getdate(), 104 )
12.09.2004


select CONVERT(varchar(12) , getdate(), 105 )
12-09-2004


select CONVERT(varchar(12) , getdate(), 106 )
12 09 2004


select CONVERT(varchar(12) , getdate(), 107 )
09 12, 2004


select CONVERT(varchar(12) , getdate(), 108 )
11:06:08


select CONVERT(varchar(12) , getdate(), 109 )
09 12 2004 1


select CONVERT(varchar(12) , getdate(), 110 )
09-12-2004


select CONVERT(varchar(12) , getdate(), 113 )
12 09 2004 1


select CONVERT(varchar(12) , getdate(), 114 )
11:06:08.177


--                                          ╔════════════════════╗
-- ================================================================================= ║ 数据库时间函数  ║
--                                          ╚════════════════════╝


-- 查询最近一个月内的点击率大于100的记录数据:
select * from t_business_product where hit_count>100 and 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


-- 你可以使用LIKE来返回正确的记录。通过在日期表达式中包含通配符“%”,
-- 你可以匹配一个特定日期的所有时间。这里有一个例子:

--这个语句可以匹配正确的记录。因为通配符“%”代表了任何时间。


Select * FROM weblog Where entrydate LIKE ‘Dec 25 2000%’


--                                          ╔════════════════════╗
-- ================================================================================= ║ CAST和CONVERT函数 ║
--                                          ╚════════════════════╝

select @@version

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-298179-1-1.html 上篇帖子: sql server如何查询一个表中的一个列是否存在唯一约束 比如查询 AA表的 BB列 下篇帖子: Sql Server中改变表中的字段名的语句!(不是教程胜似教程,是示例代码)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表