Friday, January 18, 2013

Query to get first day and last day for all the periods


DECLARE @mydate DATETIME
SELECT @mydate = GETDATE()

--Today
SELECT @mydate 'Today'

--Yesterday
SELECT DATEADD(d,-1,@mydate()) 'Yesterday'

--First Day of Current Week
SELECT DATEADD(wk,DATEDIFF(wk,0,@mydate),0) 'First Day of Current Week'

--Last Day of Current Week
SELECT DATEADD(wk,DATEDIFF(wk,0,@mydate),6) 'Last Day of Current Week'

--First Day of Last Week
SELECT DATEADD(wk,DATEDIFF(wk,7,@mydate),0) 'First Day of Last Week'

--Last Day of Last Week

SELECT DATEADD(wk,DATEDIFF(wk,7,@mydate),6) 'Last Day of Last Week'

--Last Day of Previous Month  --can also use <SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,getdate()),0))>
SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@mydate)),@mydate),101) 

--First Day of Previous Month  --can also use <SELECT DATEADD(mm, DATEDIFF(m,0,getdate())-1,0)>

SELECT CONVERT(VARCHAR(25),DATEADD(mm, DATEDIFF(m,0,@mydate)-1,0),101) 

--First Day of Current Month 

SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@mydate)-1),@mydate),101) AS Date_Value

--Last Day of Current Month  --Can also use <SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))>
SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,@mydate))),DATEADD(mm,1,@mydate)),101) 

--First Day of Next Month

SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,@mydate))-1),DATEADD(mm,1,@mydate)),101) 

--First Day of Current Quarter

SELECT CONVERT(VARCHAR(25),DATEADD(qq,DATEDIFF(qq,0,@mydate),0),101)

--Last Day of Current Quarter

SELECT CONVERT(VARCHAR(25),DATEADD(qq,DATEDIFF(qq,-1,@mydate),-1),101)

--First Day of Next Quarter

SELECT CONVERT(VARCHAR(25),DATEADD(qq, DATEDIFF(qq, 0, @mydate) + 1, 0),101)

--Last Day of Next Quarter

SELECT CONVERT(VARCHAR(25),DATEADD(s,-1,DATEADD(qq,DATEDIFF(qq,0,@mydate)+2,0)),101)

--Last Day of Next Month   --Can also use <SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+2,0))>

SELECT CONVERT(VARCHAR(25),DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@mydate)+2,0)),101)

--Last Day of a month before the lastmonth

SELECT CONVERT(VARCHAR(25),DATEADD(dd,-1,DATEADD(mm, DATEDIFF(m,0,@mydate)-1,0)),101)

--First Day of a quarter before the last completed quarter

SELECT CONVERT(VARCHAR(25),DATEADD(DD,-1,DATEADD(qq,DATEDIFF(qq,0,@mydate)-1,0)),101)

--First Day of Previous Quarter

SELECT CONVERT(VARCHAR(25),DATEADD(qq,DATEDIFF(qq,0,GETDATE())-1,0),101)

--Last Day of Previous Quarter

SELECT CONVERT(VARCHAR(25),DATEADD(DAY, -1, DATEADD(QQ, DATEDIFF(QQ, 0, GETDATE()), 0)),101)

--QuarterPart from a Date

SELECT CONVERT(VARCHAR(25),DATENAME(QUARTER,(DATEADD(DD,-1,DATEADD(qq,DATEDIFF(qq,0,@mydate)-1,0)))),101)

--YearPart from a Date

SELECT CONVERT(VARCHAR(25),YEAR(DATEADD(DD,-1,DATEADD(qq,DATEDIFF(qq,0,@mydate)-1,0))),101)

--MonthPart from a Date

SELECT CONVERT(VARCHAR(25),MONTH(DATEADD(DD,-1,DATEADD(qq,DATEDIFF(qq,0,@mydate)-1,0))),101)


--First Day of Last Year
SELECT DATEADD(yy,-1,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0))'

--Last Day of Last Year 

SELECTDATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)))


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.