Calculate time difference in HH:MM:SS - sql-server

I am trying to get the time difference between 2 times. I have a query which calculate the time in hours but what I want is to calculate in this (hh:mm) or (hh:mm:ss) format.
Is there any function or way I can achieve this ?
DECLARE #T TABLE
([EmpID] int, [TimeIn] datetime, [TimeOut] datetime);
INSERT INTO #T
([EmpID], [TimeIn], [TimeOut]) VALUES
(1, '2018-01-10 9:00:00', NULL),
(1, NULL, '2018-01-10 11:00'),
(1, '2018-01-10 11:30:00', NULL),
(1, NULL, '2018-01-10 13:00'),
(1, '2018-01-10 13:30:00', NULL),
(1, NULL, '2018-01-10 18:00'),
(1, '2018-01-11 9:00:00', NULL),
(1, NULL, '2018-01-11 11:00'),
(1, '2018-01-11 11:30:00', NULL),
(1, NULL, '2018-01-11 13:00'),
(1, '2018-01-11 13:30:00', NULL),
(1, NULL, '2018-01-11 18:33')
;
SELECT
FirstIN = CAST(MIN([AttendanceTimeIn]) AS TIME),
LastOUT = CAST(MAX([AttendanceTimeOut]) AS TIME),
HoursSpent = DATEDIFF(HOUR, CAST(MIN(AttendanceTimeIn) AS TIME), CAST(MAX(AttendanceTimeOut) AS TIME)),
CAST(COALESCE(AttendanceTimeIn, AttendanceTimeOut) AS DATE) [Date]
FROM #T
GROUP BY
CAST(COALESCE(AttendanceTimeIn, AttendanceTimeOut) AS DATE)

You can use this for With DATETIME
DECLARE #START_DATE datetime DECLARE #END_DATE datetime
-- Set the timeframe variables
SET #START_DATE = '2011-01-01 16:00:00' SET #END_DATE = '2011-01-01 22:47:22'
-- Use datediff to show the results in the
-- HH:MM:SS format
SELECT CONVERT(VARCHAR(6), datediff(second, #START_DATE, #END_DATE)/3600) + ':'
+ RIGHT('0' + CONVERT(VARCHAR(2), (datediff(second, #START_DATE, #END_DATE) %
3600) / 60), 2) + ':' + RIGHT('0' + CONVERT(VARCHAR(2), datediff(second,
#START_DATE, #END_DATE) % 60), 2)
Result :
HH:MM:SS
6:47:22
OR With TIME
-- Declare some variables for the requested timeframe
DECLARE #START_DATE TIME
DECLARE #END_DATE TIME-- Set the timeframe variables
SET #START_DATE = '16:00:00'
SET #END_DATE = '22:47:22'-- Use datediff to show the results in the
-- HH:MM:SS format
SELECT CONVERT(VARCHAR(6), Datediff(second, #START_DATE, #END_DATE)/3600)
+ ':'
+ RIGHT('0' + CONVERT(VARCHAR(2), (Datediff(second, #START_DATE,
#END_DATE) %
3600) / 60), 2)
+ ':'
+ RIGHT('0' + CONVERT(VARCHAR(2), Datediff(second, #START_DATE, #END_DATE
) % 60)
, 2) AS 'HH:MM:SS'
AS Per Edit
;
WITH cte
AS (SELECT EMPID,
Min(TIMEIN) timein,
Max(TIMEOUT) timeout,
Cast(COALESCE(TIMEIN, TIMEOUT) AS DATE) d
FROM #T
GROUP BY EMPID,
Cast(COALESCE(TIMEIN, TIMEOUT) AS DATE))
SELECT EMPID,
D AS Day,
CONVERT(VARCHAR(6), Datediff(second, TIMEIN, TIMEOUT)/3600)
+ ':'
+ RIGHT('0' + CONVERT(VARCHAR(2), (Datediff(second, TIMEIN, TIMEOUT) %
3600) /
60), 2)
+ ':'
+ RIGHT('0' + CONVERT(VARCHAR(2), Datediff(second, TIMEIN, TIMEOUT) % 60)
, 2 )
AS [Hours Worked]
FROM cte
ORDER BY D ASC
Result
empid Day Hours Worked
1 2018-01-10 9:00:00
1 2018-01-11 9:33:00
Resolve After Mistake of preiviuos senario
SELECT FirstIN, LastOUT,
CONVERT(VARCHAR(6), Datediff(second, FirstIN, LastOUT)/3600)
+ ':'
+ RIGHT('0' + CONVERT(VARCHAR(2), (Datediff(second, FirstIN, LastOUT) %
3600) /
60), 2)
+ ':'
+ RIGHT('0' + CONVERT(VARCHAR(2), Datediff(second, FirstIN, LastOUT) % 60)
, 2 ) AS
HoursSpent,[Date] FROM (
SELECT
FirstIN = CAST(MIN([TimeIn]) AS TIME),
LastOUT = CAST(MAX([TimeOut]) AS TIME),
HoursSpent = DATEDIFF(HOUR, CAST(MIN(TimeIn) AS TIME), CAST(MAX(TimeOut) AS TIME)),
CAST(COALESCE(TimeIn, TimeOut) AS DATE) [Date]
FROM #T
GROUP BY
CAST(COALESCE(TimeIn, TimeOut) AS DATE)) AS a

Check this snippet which returns difference at seconds level (HH:mm:ss)
SELECT CONVERT(time,
DATEADD(s,
DATEDIFF(s,
'2018-01-07 09:53:00',
'2018-01-07 11:53:01'),
CAST('1900-01-01 00:00:00.0000000' as datetime2)
)
)
If you need HH:mm only change 's' for 'mi'
And if you need difference in days too use this one:
SELECT CONVERT(datetime2,
DATEADD(s,
DATEDIFF(s,
'2018-01-07 09:53:00',
'2018-01-07 11:53:01'),
CAST('0001-01-01 00:00:00.0000000' as datetime2)
)
)

Related

Calculate Time Difference in hh:mm

I have this query but I'm not sure what function to use so I can get time difference in this "hh:mm" format. Can anybody help ?
SELECT
EI.[FirstName]+' '+EI.[LastName] [EmployeeName], [Dpt].[FullName] [Department], [Desig].[FullName] [Designation],
FirstIN = CAST(MIN([AttendanceTimeIn]) AS TIME),
LastOUT = CAST(MAX([AttendanceTimeOut]) AS TIME),
HoursSpent = DATEDIFF(HOUR, CAST(MIN(AttendanceTimeIn) AS TIME), CAST(MAX(AttendanceTimeOut) AS TIME)),
CAST(COALESCE(AttendanceTimeIn, AttendanceTimeOut) AS DATE) [Date]
FROM [HRM].[tbl_Designation] [Desig], [HRM].[tbl_Department] [Dpt], [HRM].[tbl_EmployeeInfo] [EI]
FULL OUTER JOIN [HRM].[tbl_EmployeeAttendance] [Attendance] ON [Attendance].[EmpCode] = [EI].[ID]
WHERE
[Dpt].[ID] = [EI].[DeptCode] AND [Desig].[ID] = [EI].[DesignationCode]
AND
[EI].[RecordStatusCode] != '13'
AND
CAST((GETDATE()-1) AS DATE) = CAST(ISNULL([AttendanceTimeIn], [AttendanceTimeOut]) AS Date)
GROUP BY
EI.[FirstName]+' '+EI.[LastName], [Dpt].[FullName], [Desig].[FullName], CAST(COALESCE(AttendanceTimeIn, AttendanceTimeOut) AS DATE)
Maybe you can use something like this:
declare #d1 datetime = '2018-01-11 23:40:18.010'
declare #d2 datetime = '2018-01-12 11:59:18.010'
SELECT CONVERT(VARCHAR(2),DATEPART(HOUR,#d2 - #d1)) + ':' + CONVERT(VARCHAR(2),DATEPART(MINUTE,#d2 - #d1));
If to leave out your concept of using the time for the duration, you can use a query like below:
DECLARE #d1 DATETIME
DECLARE #d2 DATETIME
SELECT #d1 = GETDATE() - 1.465, #d2 = GETDATE()
SELECT
FirstIN = #d1,
LastOUT = #d2,
HoursSpent = CAST(DATEDIFF(HOUR, #d1, #d2) AS VARCHAR) + ':' + RIGHT('0' + CAST(DATEDIFF(MINUTE, #d1, #d2) % 60 AS VARCHAR), 2)
Let's expand step by step:
declare
#start datetime = getdate(),
#end datetime = dateadd(second, getdate(), 5000)
-- datediff(hour, #start, #end) -> hour difference
-- dateadd(hour, #start, datediff(hour, #start, #end)) -> start + integer hour difference to get less-than-an-hour diff
-- datediff(minute, dateadd(hour, #start, datediff(hour, #start, #end)), #end) -> minute difference from there
select
cast(datediff(hour, #start, #end) as varchar(2))
+ ':'
+ cast(datediff(minute, dateadd(hour, #start, datediff(hour, #start, #end)), #end) as varchar(2)) as [hh:mm]

SQL server date time

I am trying to get object which its start date + start time have passed or equal to current datetime and its end date + end time hasn't pass the current datetime. I tried convert current datetime to nvarchar as all my columns are nvarchar. I also tried converting my columns to datetime type but it doesn't work too. Please help
Column:
startDate - nvarchar (dd/mm/yyyy)
startTime - nvarchar (hh:mm) 24 hr
endDate - nvarchar(dd/mm/yyyy)
endTime - nvarchar(hh:mm) 24hr
i did try to but datetime betwween startDate + startTime AND endDate +startTime,
but it doesn't seems to work :
SELECT * FROM Promo
WHERE membership = '1'
AND promoStatus = '1'
AND CONVERT(NVARCHAR, GetDate(), 101) + ' ' +
CONVERT(NVARCHAR, DATEPART(hh, GetDate())) + ':' +
RIGHT('0' + CONVERT(NVARCHAR, DATEPART(mi, GetDate())), 2) BETWEEN
startDate + ' ' + startTime
AND endDate + ' ' + endTime
i did try other method too :
SELECT * FROM Promo
WHERE membership = '1'
AND promoStatus = '1'
AND startDate + ' ' + startTime <= CONVERT(NVARCHAR, GetDate(), 101) + ' ' +
CONVERT(NVARCHAR, DATEPART(hh, GetDate())) + ':' +
RIGHT('0' + CONVERT(NVARCHAR, DATEPART(mi, GetDate())), 2)
AND endDate + ' ' + endTime >= CONVERT(NVARCHAR, GetDate(), 101) + ' ' +
CONVERT(NVARCHAR, DATEPART(hh, GetDate())) + ':' +
RIGHT('0' + CONVERT(NVARCHAR, DATEPART(mi, GetDate())), 2)
You could do it like this:
SELECT *
FROM Promo
WHERE
membership = '1'
AND promoStatus = '1'
AND CAST(CONVERT(VARCHAR(16), GETDATE(), 120) AS DATETIME) BETWEEN
CONVERT(DATETIME, #startDate, 103) + CONVERT(DATETIME, #startTime)
AND CONVERT(DATETIME, #endDate, 103) + CONVERT(DATETIME, #endTime)
This line:
CAST(CONVERT(VARCHAR(16), GETDATE(), 120) AS DATETIME)
gets the current date time up to its minutes, meaning this will strip off the seconds and milliseconds part of the GETDATE().
And this line:
CONVERT(DATETIME, #startDate, 103) + CONVERT(DATETIME, #startTime)
will combine your date and time variables to form a new DATETIME value.
Note that you could have done away with with this if you design the tables using the proper data types.
Try this
SELECT * FROM Promo
WHERE membership = '1'
AND promoStatus = '1'
AND CAST(CONVERT(VARCHAR(16), GetDate(), 120) AS DATETIME)
BETWEEN CONVERT(DATETIME, startDate + ' ' + startTime, 120)
AND CONVERT(DATETIME, endDate + ' ' + endTime, 120)

How to get Hour and minutes in decimal format from two dates

Here is my query
select DATEDIFF(minute, '2015-06-10 14:00:00.000', '2015-06-10 16:50:00.000') / 60.0
I get 2.833333 but i want 2.50
This gives you what you asked for, but please keep in mind that usually 2.50 is equal to 2.5 and means 5/2; you are using a notation that it is quite uncommon
DECLARE #f float
DECLARE #t nvarchar(15)
SELECT #f = DATEDIFF(hour, '2015-06-10 14:00:00.000', '2015-06-10 16:50:00.000')
SELECT #t = CONVERT(nvarchar(10), #f) + N'.' + CONVERT(nvarchar(10), (DATEDIFF(minute, '2015-06-10 14:00:00.000', '2015-06-10 16:50:00.000') - #f*60))
SELECT #t
So you want to show the hours and minutes? You could use this:
DECLARE #t1 datetime
SET #t1 = '2015-06-10 14:00:00.000'
DECLARE #t2 datetime
SET #t2 = '2015-06-10 16:50:00.000'
SELECT HHmm = CONVERT(varchar(3), DATEDIFF(minute, #t1, #t2) / 60)
+ '.'
+ RIGHT('0' + CONVERT(varchar(2), DATEDIFF(minute, #t1, #t2) % 60), 2)
Demo
The DateDiff function returns an integer. to display it as time you need to use some math and casting:
DECLARE #FromDate datetime = '2015-06-10 14:00:00.000',
#ToDate datetime = '2015-06-10 16:50:00.000'
select CAST(DATEDIFF(minute, #FromDate, #ToDate) / 60 as varchar) + ':' +
CAST(DATEDIFF(minute, #FromDate, #ToDate) % 60 as varchar)

Getting Error when converting to datetime :Date Column + Time Column

Below is my sql query, Inner query returning the value but outer query returning error:
"Conversion failed when converting character string to smalldatetime data type. "
SELECT x.*
FROM (
SELECT
Convert(SMALLDATETIME, Convert(VARCHAR(30), Convert(DATE, Event_Date, 101)) +
Convert(VARCHAR(30), ' ') + Convert(VARCHAR(30), Convert(TIME, Event_Time)), 101)
,[Event_Date]
,Location
,Street
,City
,[State]
,Country
,ZipCode
,[Subject]
,[Detail]
,[LeadInstructor]
,[CoInstructor]
FROM [Temp_EVT_EVENT]
) x
SMALLDATE can accept time value upto 18:00:00. But you used TIME format 18:00:00.000000 that is why it show error. Check the query and update the status.
First run this query separately:
SELECT
Convert(SMALLDATETIME, Convert(VARCHAR(30), Convert(DATE, CAST('2014-11-14' AS DATETIME), 101)) +
Convert(VARCHAR(30), ' ') + Convert(VARCHAR(30), CONVERT(VARCHAR(8), CAST('18:13:04' AS DATETIME), 108)), 101) AS column1
if you get the answer. '2014-11-14 18:13:00'
Then try to run:
SELECT
Convert(SMALLDATETIME, Convert(VARCHAR(30), Convert(DATE, CAST(Event_Date AS DATETIME), 101)) +
Convert(VARCHAR(30), ' ') + Convert(VARCHAR(30), CONVERT(VARCHAR(8), CAST(Event_Time AS DATETIME), 108)), 101) AS column1 FROM Temp_EVT_EVENT
if you get the answer correctly. Then Run:
SELECT x.*
FROM (
SELECT
Convert(SMALLDATETIME, Convert(VARCHAR(30), Convert(DATE, CAST(Event_Date AS DATETIME), 101)) +
Convert(VARCHAR(30), ' ') + Convert(VARCHAR(30), CONVERT(VARCHAR(8), CAST(Event_Time AS DATETIME), 108)), 101) AS column1
,[Event_Date]
,Location
,Street
,City
,[State]
,Country
,ZipCode
,[Subject]
,[Detail]
,[LeadInstructor]
,[CoInstructor]
FROM [Temp_EVT_EVENT]
) x
Basing on your Event_Date format, look here: Date Formats
It seems that the correct format is 120, and not 101.
So:
SELECT x.*
FROM (
SELECT
Convert(SMALLDATETIME, Convert(VARCHAR(30), Convert(DATE, Event_Date, 120)) +
Convert(VARCHAR(30), ' ') + Convert(VARCHAR(30), Convert(TIME, Event_Time)), 120)
,[Event_Date]
,Location
,Street
,City
,[State]
,Country
,ZipCode
,[Subject]
,[Detail]
,[LeadInstructor]
,[CoInstructor]
FROM [Temp_EVT_EVENT]
) x

PL/SQL to TSQL conversion

I have used a tool to convert PL/SQL to TSQL but I am getting weird errors for some cases.
CREATE PROC Dates
(
#PeriodType varchar(15),
#ReportStart varchar(15),
#ReportEnd varchar(15)
)
AS
BEGIN
SELECT CASE
WHEN #PeriodType = 'CUR_WEEK' THEN CONVERT(DATETIME, CONVERT(VARCHAR, GETDATE() + 1 - CONVERT(NUMERIC(8, 2), CONVERT(VARCHAR(23), GETDATE())), 112))
WHEN #PeriodType = 'NEXT_WEEK' THEN CONVERT(DATETIME, CONVERT(VARCHAR, GETDATE() + 8 - CONVERT(NUMERIC(8, 2), CONVERT(VARCHAR(23), GETDATE())), 112))
WHEN #PeriodType = 'PREV_DAY' THEN CONVERT(DATETIME, CONVERT(VARCHAR, GETDATE() - 1, 112))
WHEN #PeriodType = 'PREV_WEEK' THEN CONVERT(DATETIME, CONVERT(VARCHAR, GETDATE() - 6 - CONVERT(NUMERIC(8, 2), CONVERT(VARCHAR(23), GETDATE())), 112))
WHEN #PeriodType = 'PREV_2WEEK' THEN CONVERT(DATETIME, CONVERT(VARCHAR, GETDATE() - 13, 112))
WHEN #PeriodType = 'CUR_MONTH' THEN CONVERT(DATETIME, CAST(CONVERT(VARCHAR(23), GETDATE()) AS VARCHAR) + '01', 112)
WHEN #PeriodType = 'DATE_RANGE' THEN CONVERT(DATETIME, #ReportStart)
END AS "StartDate",
CASE
WHEN #PeriodType = 'CUR_WEEK' THEN CONVERT(DATETIME, CONVERT(VARCHAR, GETDATE() + 8 - CONVERT(NUMERIC(8, 2), CONVERT(VARCHAR(23), GETDATE())), 112)) - (1 / CONVERT(FLOAT, 86400))
WHEN #PeriodType = 'NEXT_WEEK' THEN CONVERT(DATETIME, CONVERT(VARCHAR, GETDATE() + 15 - CONVERT(NUMERIC(8, 2), CONVERT(VARCHAR(23), GETDATE())), 112)) - (1 / CONVERT(FLOAT, 86400))
WHEN #PeriodType = 'PREV_DAY' THEN CONVERT(DATETIME, CONVERT(VARCHAR, GETDATE(), 112)) - (1 / CONVERT(FLOAT, 86400))
WHEN #PeriodType = 'PREV_WEEK' THEN CONVERT(DATETIME, CONVERT(VARCHAR, GETDATE() - CONVERT(NUMERIC(8, 2), CONVERT(VARCHAR(23), GETDATE())), 112)) + 1 - (1 / CONVERT(FLOAT, 86400))
WHEN #PeriodType = 'PREV_2WEEK' THEN CONVERT(DATETIME, CONVERT(VARCHAR, GETDATE() + 1, 112)) - (1 / CONVERT(FLOAT, 86400))
WHEN #PeriodType = 'CUR_MONTH' THEN DATEADD(M, 1, CONVERT(DATETIME, CONVERT(VARCHAR, GETDATE(), 112)) - (CONVERT(NUMERIC(8, 2), DATEPART(DD, GETDATE())) - 1)) - 1
WHEN #PeriodType = 'DATE_RANGE' THEN CONVERT(DATETIME, #ReportEnd) + 1
END AS "EndDate"
END
My stored proc is executing for PREV_DAY and PREV_2WEEK for the remaining period type I am getting conversions errors.
exec proc Dates 'PREV_DAY','',''
exec proc Dates 'PREV_2WEEK','',''
My PL\SQL code is
SELECT CASE
WHEN :PeriodType = 'CUR_WEEK'
THEN TRUNC(SYSDATE + 1 - TO_NUMBER(TO_CHAR(SYSDATE, 'D')))
WHEN :PeriodType = 'NEXT_WEEK'
THEN TRUNC(SYSDATE + 8 - TO_NUMBER(TO_CHAR(SYSDATE, 'D')))
WHEN :PeriodType = 'PREV_DAY'
THEN TRUNC(SYSDATE - 1)
WHEN :PeriodType = 'PREV_WEEK'
THEN TRUNC(SYSDATE - 6 - TO_NUMBER(TO_CHAR(SYSDATE, 'D')))
WHEN :PeriodType = 'PREV_2WEEK'
THEN TRUNC(SYSDATE - 13)
WHEN :PeriodType = 'CUR_MONTH'
THEN to_date(to_char(sysdate,'YYYYMM') || '01','YYYYMMDD')
WHEN :PeriodType = 'DATE_RANGE'
THEN TO_DATE(:ReportStart)
END AS "StartDate",
CASE
WHEN :PeriodType = 'CUR_WEEK'
THEN TRUNC(SYSDATE + 8 - TO_NUMBER(TO_CHAR(SYSDATE, 'D'))) - (1/86400)
WHEN :PeriodType = 'NEXT_WEEK'
THEN TRUNC(SYSDATE + 15 - TO_NUMBER(TO_CHAR(SYSDATE, 'D'))) - (1/86400)
WHEN :PeriodType = 'PREV_DAY'
THEN TRUNC(SYSDATE)-(1/86400)
WHEN :PeriodType = 'PREV_WEEK'
THEN TRUNC(SYSDATE - TO_NUMBER(TO_CHAR(SYSDATE, 'D'))) + 1 - (1/86400)
WHEN :PeriodType = 'PREV_2WEEK'
THEN TRUNC ( SYSDATE+1) - (1/86400)
WHEN :PeriodType = 'CUR_MONTH'
THEN ADD_MONTHS(TRUNC(SYSDATE) - (TO_NUMBER(TO_CHAR(SYSDATE,'DD')) - 1), 1) -1
WHEN :PeriodType = 'DATE_RANGE'
THEN TO_DATE(:ReportEnd)+1
END AS "EndDate"
FROM DUAL
At least one problem is this expression in the first then clause:
CONVERT(NUMERIC(8, 2), CONVERT(VARCHAR(23), GETDATE()))
This expression:
select CONVERT(VARCHAR(23), GETDATE())
just returned:
May 23 2014 3:01AM
And that just won't convert to numeric.
I think you need to go through the code line by line to figure out the best way to do things. Also, whenever you use varchar or char, you should have a length associated with them. The default is sometimes (but not always) 1, and that can cause problems.
Oracle and SQLServer work with datetime in different manner.
The tool converted the script as well as a string parser can, like someone translating a quote word by word between two languages.
In SQLServer to work with that data type you have to use the functions DATEADD, DATEDIFF and other ones that can be found on the help page from MS.
Using the date function of SQLServer the original function translate to
CREATE PROC Dates(
#PeriodType varchar(15),
#ReportStart varchar(15),
#ReportEnd varchar(15)
)
AS
BEGIN
SELECT CASE WHEN #PeriodType = 'CUR_WEEK' THEN DATEADD(DAY, - DATEPART(dw, GETDATE()) + 1, cast(cast(GETDATE() AS date) AS datetime))
WHEN #PeriodType = 'NEXT_WEEK' THEN DATEADD(DAY, 8 - DATEPART(dw, GETDATE()), cast(cast(GETDATE() AS date) AS datetime))
WHEN #PeriodType = 'PREV_DAY' THEN DATEADD(DAY, -1, cast(cast(GETDATE() AS date) AS datetime))
WHEN #PeriodType = 'PREV_WEEK' THEN DATEADD(DAY, - DATEPART(dw, GETDATE()) - 6, cast(cast(GETDATE() AS date) AS datetime))
WHEN #PeriodType = 'PREV_2WEEK' THEN DATEADD(DAY, - 13, cast(cast(GETDATE() AS date) AS datetime))
WHEN #PeriodType = 'CUR_MONTH' THEN DATEADD(DAY, - DATEPART(DAY, GETDATE()) + 1, cast(cast(GETDATE() AS date) AS datetime))
WHEN #PeriodType = 'DATE_RANGE' THEN CONVERT(DATETIME, #ReportStart)
END AS StartDate,
, CASE WHEN #PeriodType = 'CUR_WEEK' THEN DATEADD(mi, -1, DATEADD(DAY, 8 - DATEPART(dw, GETDATE()), cast(cast(GETDATE() AS date) AS datetime)))
WHEN #PeriodType = 'NEXT_WEEK' THEN DATEADD(mi, -1, DATEADD(DAY, 15 - DATEPART(dw, GETDATE()), cast(cast(GETDATE() AS date) AS datetime)))
WHEN #PeriodType = 'PREV_DAY' THEN DATEADD(mi, -1, DATEADD(DAY, - DATEPART(dw, GETDATE()) + 1, cast(cast(GETDATE() AS date) AS datetime)))
WHEN #PeriodType = 'PREV_WEEK' THEN DATEADD(mi, -1, DATEADD(DAY, 8 - DATEPART(dw, GETDATE()), cast(cast(GETDATE() AS date) AS datetime)))
WHEN #PeriodType = 'PREV_2WEEK' THEN DATEADD(mi, -1, cast(cast(dateadd(DAY, 1, GETDATE()) AS date) AS datetime))
WHEN #PeriodType = 'CUR_MONTH' THEN DATEADD(DAY, -1, DATEADD(mm, DATEDIFF(mm, 0, cast(cast(GETDATE() AS date) AS datetime))+1,0))
WHEN #PeriodType = 'DATE_RANGE' THEN CONVERT(DATETIME, #ReportEnd) + 1
END AS EndDate
END
The double CAST in CAST(CAST(GETDATE() AS DATE) AS DATETIME) is a little hack to get midnight of today, as GETDATE() is of datetime type and returns the current date and time.

Resources