使用DATETIME函数得到解决的情况 [英] Got a situation to solve using DATETIME function

查看:72
本文介绍了使用DATETIME函数得到解决的情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个日期(例如2000年1月30日),我需要找到该年的第一个和最后一个日期,然后是该月的第一个和最后一个日期,以及该周的第一个和最后一个日期.我该怎么做?

Given a date (say 30-Jan- 2000), I need to find the first and last date of that year, then the first and last date of the month, and the first and last date of that week. How do i do this?

推荐答案

嗨 对于今年的开始和结束
Hi For Start and end of the year
select dateadd(d,-datepart(dy,getdate())+1,getdate()) as yearstart,
dateadd(d,-datepart(d,getdate()),dateadd(m,13-datepart(m,getdate()),getdate())) as yearend



对于本月的开始和结束



For Start and end of the month

select dateadd(day,-(day('2012-10-01')-1),'2012-10-01')
select dateadd(day,-1,dateadd(month,1,dateadd(day,-(day('2012-10-01')-1),'2012-10-01')))



对于一周的第一天和最后一天



For first day nad last day of week

select DATEADD(DAY, 1- DATEPART(DW, '2012-10-12'), '2012-10-12')
SELECT  DATEADD(DAY, 7- DATEPART(DW, '2012-10-12'), '2012-10-12')


这篇关于使用DATETIME函数得到解决的情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆