Retrieving time period t-sql - sql

I have a stored procedure set up in such a way as to get the results for today
so
DECLARE #Start DATETIME
DECLARE #End DATETIME
SET #Start = DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
SET #End = DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 1)
PRINT #Start
PRINT #End
gives me the time period
May 30 2016 12:00AM
May 31 2016 12:00AM
How can I modify the clause as to give me the period
May 29 2016 5:30PM(Previous day)
May 31 2016 12:00AM(Today)
Kind regards

Try this solution:
SET #Start = DATEADD(MINUTE, 30, DATEADD(HOUR, 17, DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -1)))
SET #End = DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 1)

DECLARE #Start DATETIME
DECLARE #End DATETIME
SET #Start = GETDATE()-1
SET #End = DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 1)
PRINT #Start
PRINT #End

dateadd(day, -1, getdate())
May be just use the DateAdd like above.

Related

Loop through the same query to build historical table changing one variable

I have read only access to SQL Server 2008 and I need to build a historical table of percentages based on different points of time in the past (i.e. the last 15 months). The only value that needs to be changed is on the *** line is -0 after GETDATE()). The value needs to change -0, -1, -2...-15. I am inserting into a temp table. Any suggestions would be appreciated.
declare #enddate datetime
declare #end1195 datetime
declare #beg1195 datetime
declare #enddos datetime
declare #begdos datetime
declare #period nvarchar(7)
***set #enddate = DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-0, -1)
set #end1195 = DATEADD(MONTH, DATEDIFF(MONTH, -1, (#enddate))-1, -1)
set #beg1195 = DATEADD(MONTH, DATEDIFF(MONTH, 0, dateadd(month, -6, #enddate)), 0)
set #enddos = DATEADD(MONTH, DATEDIFF(MONTH, -1, (#enddate))-0, -1)
set #begdos= DATEADD(MONTH, DATEDIFF(MONTH, 0, dateadd(month, -0, #enddate)), 0)
set #period = cast(MONTH(#enddate) as nvarchar(2)) + '-' + cast(YEAR(#enddate) as nvarchar(4))
--1195 BY Facilityid excluding ANES
insert into ##Fac1195
select
A.Facilityid, cast(1 as int) as Facility,
A.FSC_DESC, #period as Period, #begdos as DOS_Beg,
#enddos as DOS_End,
ROUND(sum(A.adjustments)/sum(A.charges), 2) as [1195]
from
(select
v.Facilityid, v.localfacility, v.VisitID, v.AccountNumber,
v.DOS, FSC.PrimayPayer as PayerName,
FSC.PrimaryCorpFSC as FSC_DESC,
chg.charges, isnull(adj.Adjustments,0) as Adjustments
........

Prior Month for Prior Year

I have been able to Declare and Set grabbing the prior months data.
DECLARE #FirstDayofPrevMonth datetime
SET #FirstDayofPrevMonth = DATEADD(mm, DATEDIFF(m,0,GETDATE())-1,0)
DECLARE #LastDayofPrevMonth datetime
SET #LastDayofPrevMonth = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))
What I now need is to be able to set and declare prior month, prior year....
to be clear, this does work for me. I get the expected result which is Aug 2014. What I'm looking to do, is get Aug 2013 and so on.
UPDATE:
I thought I would share two other pieces I added to my code that may be helpful to others.
I used the below answer and added to it
DECLARE #FirstDayofPrevMonth datetime
SET #FirstDayofPrevMonth = DATEADD(mm, DATEDIFF(m,0,GETDATE())-1,0)
DECLARE #LastDayofPrevMonth datetime
SET #LastDayofPrevMonth = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))
-- to be used for the prior year completed month
DECLARE #FirstDayofPrevMonthLY datetime
SET #FirstDayofPrevMonthLY = DATEADD(year,-1,#FirstDayofPrevMonth)
DECLARE #LastDayofPrevMonthLY datetime
SET #LastDayofPrevMonthLY = DATEADD(year,-1,#LastDayofPrevMonth)
--This is to be used if you want for dates in the current Quarter
DECLARE #FirstDayofCQ datetime
SET #FirstDayofCQ = DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0)
DECLARE #LastDayofCQ datetime
SET #LastDayofCQ =DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) + 1, 0)
--This is to be used for the Prior Quarter
DECLARE #FirstDayofPrevQ datetime
SET #FirstDayofPrevQ = DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 1, 0)
DECLARE #LastDayofPrevQ datetime
SET #LastDayofPrevQ = DATEADD(dd, -1, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0))
DECLARE #FirstDayofPrevMonthPriorYear datetime
DECLARE #LastDayofPrevMonthPriorYear datetime
SET #FirstDayofPrevMonthPriorYear = DATEADD(year,-1,#FirstDayofPrevMonth)
SET #LastDayofPrevMonthPriorYear = DATEADD(year,-1,#LastDayofPrevMonth)
DECLARE #FirstDayofPrevMonthPrevYear datetime
SET #FirstDayofPrevMonthPrevYear = (DATEADD(yy, -1, DATEADD(mm, DATEDIFF(m,0,GETDATE())-1,0)))

How can i find the particular days

I have the date value like this - 12/2011 or 11/2011 (MM/yyyy)
I want to find the sundays on the particular month....
For Example, If i select the month 01/2012, The query should give the result like this
01
08
15
22
29
The above date are sunday.
Expected Output for 01/2012 Month
01
08
15
22
29
How to make a query
Need Query Help
With a little help of a number table (master..spt_values)
declare #M varchar(7)
set #M = '01/2012'
declare #D datetime
set #D = convert(datetime, '01/'+#M, 103)
set datefirst 7
select dateadd(day, N.Number, #D)
from master..spt_values as N
where N.type = 'P' and
dateadd(day, N.Number, #D) >= #D and
dateadd(day, N.Number, #D) < dateadd(month, 1, #D) and
datepart(weekday, dateadd(day, N.Number, #D)) = 1
Here it comes:
SET DATEFIRST 1
DECLARE #givenMonth CHAR(7)
SET #givenMonth = '12/2011'
DECLARE #month INT
SET #month = CAST(SUBSTRING(#givenMonth, 1, 2) AS INT)
DECLARE #year INT
SET #year = CAST(SUBSTRING(#givenMonth, 4, 4) AS INT)
DECLARE #Date DATETIME
SET #Date = DATEADD(month, #month - 1, CAST(CAST(#year AS CHAR(4)) AS DATETIME))
DECLARE #nextMonth DATETIME
SET #nextMonth = DATEADD(MONTH, 1, #date)
DECLARE #firstSunday DATETIME
SET #firstSunday = DATEADD(day, 7 - DATEPART(weekday, #date), #date)
CREATE TABLE #Days(Sunday INT)
WHILE #firstSunday < #nextMonth
BEGIN
INSERT #Days
SELECT DATEPART(DAY, #firstSunday) Sunday
SET #firstSunday = DATEADD(day, 7, #firstSunday)
END
SELECT Sunday
FROM #Days
ORDER BY 1
DROP TABLE #Days
Edit: use a numbers table in place of the CTE because you are in SQL Server 2000. C'mon, upgrade and do yourself a favour
DECLARE #monthyear varchar(10) = '11/2012';
DECLARE #start smalldatetime, #end smalldatetime;
-- use yyyymmdd format
SET #start = CAST(RIGHT(#monthyear, 4)+ LEFT(#monthyear, 2) + '01' AS smalldatetime);
-- work backwards from start of next month
SET #end = DATEADD(day, -1, DATEADD(month, 1, #start));
-- recursive CTE. Would be easier with a numbers table
;WITH daycte AS
(
SELECT #start AS TheDay
UNION ALL
SELECT DATEADD(day, 1, TheDay)
FROM daycte
WHERE TheDay < #end
)
SELECT DATENAME(day, TheDay)
FROM daycte
-- One of many ways.
-- This is independent of ##datefirst but fails with Chinese and Japanese language settings
WHERE DATENAME(weekday, TheDay) = 'Sunday';
You can change date format and use DateName function.
SELECT DateName(dw, GETDATE())

How to get the last month data and month to date data

Need help in writing the query to get the last month data as well as month to date data.
If today's date is Mar 23 2011, I need to retrieve the data from last month and the data till todays date(means Mar 23 2011).
If date is Apr 3 2011, data should consists of March month data and the data till Apr 3rd 2011.
Thanks,
Shahsra
Today including time info : getdate()
Today without time info : DATEADD(DAY, DATEDIFF(day, 0, getdate()), 0)
Tomorrow without time info : DATEADD(DAY, DATEDIFF(day, 0, getdate()), 1)
Beginning of current month : DATEADD(month, datediff(month, 0, getdate()), 0)
Beginning of last month : DATEADD(month, datediff(month, 0, getdate())-1, 0)
so most likely
WHERE dateColumn >= DATEADD(month, datediff(month, 0, getdate())-1, 0)
AND dateColumn < DATEADD(DAY, DATEDIFF(day, 0, getdate()), 1)
Step back one month, subtract the number of days to the current date, and add one day.
WHERE
DateField <= GetDate() AND
DateField >= DateAdd(
mm,
-1,
DateAdd(dd, -1*DatePart(dd, GetDate())+1, GetDate())
)
To remove the time quickly, you can use this
Cast( Floor( Cast( GETDATE() AS FLOAT ) ) AS DATETIME )
So the second part would be (without time)
DateField >= Cast( Floor( Cast( (DateAdd(
mm,
-1,
DateAdd(dd, -1*DatePart(dd, GetDate())+1, GetDate())
)) AS FLOAT ) ) AS DATETIME )
Select Column1, Column2 From Table1
Where DateColumn <= GetDate() AND
DateColumn >= DATEADD(dd, - (DAY(DATEADD(mm, 1, GetDate())) - 1), DATEADD(mm, - 1, GetDate()))
Edit: +1 to Russel Steen. I was posting mine before I knew he had posted.
Very helpful page
declare #d datetime = '2011-04-03';
declare #startDate datetime;
select #startDate =
CAST('01 '+ RIGHT(CONVERT(CHAR(11),DATEADD(MONTH,-1,#d),113),8) AS datetime);
select #startDate;

How do I add Hours, mins, seconds to dateadd sql?

I want to get an entire date
So today would be 7/7/2010 12:00:00 am to 7/7/2010 11:59:59 pm
So that should be the full 24 hours since 12:00:00 am would be the 8th then.
So I have this
select DATEADD(??, ??, DATEDIFF(dd, 0, GETUTCDATE()))
How do I make it add 23 hours 59mins and 59seconds to it?
DECLARE #start DATETIME
DECLARE #end DATETIME
SET #start = DATEADD(dd, 0, DATEDIFF(dd, 0, GETUTCDATE()))
SET #end = DATEADD(dd, 1, DATEADD(ms, -3, #start))
Try this:
DATEADD(second, -1, DATEADD(DAY, 1,"7/7/2010 12:00:00"))