I'm modifying a store procedure in SQL Server 2008. The original procedure took in two dates
#StartDate DATETIME
#EndDate DATETIME
And would convert the time portion to the Earliest and latest possible times respectively.
I have added two additional parameters to accept Time Portions.
#StartTime DATETIME
#EndTime DATETIME
The Time portions are optional.
An RDL report file generates the report online for the users. The logic needs to occur in the Stored Proc.
What I have so far is not much, as I'm a C# programmer leaving my element.
IF (#StartTime IS NULL)
SET #StartDate = fn_SetBeginningTime(#StartDate) -- Sets time portion to earliest value
ELSE
-- This is where I don't know how to add the time from #StartTime to the time portion of the datetime value #StartDate
IF (#EndTime IS NULL)
-- This will do the same as the start time/start date
Assuming:
By earliest you mean 00:00:00 on the start date
By latest you mean 00:00:00 on the day after enddate (for use with <=)
Should there be any, this will ignore the time from a #*Date param and the date from a#*Time param.
declare #StartDate DATETIME = '15 jul 2010'
declare #EndDate DATETIME = '15 jul 2010'
declare #StartTime DATETIME = '06:06:06'
declare #EndTime DATETIME = null
--always make #StartDate/#EndDate's time 00:00:00
SET #StartDate = CAST(#StartDate AS DATE)
SET #EndDate = CAST(#EndDate AS DATE)
IF (#StartTime IS NOT NULL) -- set #StartDate's time to #StartTime
SET #StartDate += CAST(#StartTime AS TIME)
IF (#EndTime IS NULL)
SET #EndDate += 1 --set it to midnight
ELSE
SET #EndDate += CAST(#EndTime AS TIME) --set #EndDate's time to #EndTime
select #StartDate, #EndDate
>>> 2010-07-15 06:06:06.000,2010-07-16 00:00:00.000
For DATE / TIME;
declare #StartDate DATE = '15 jul 2010'
declare #EndDate DATE = '15 jul 2010'
declare #StartTime TIME = null
declare #EndTime TIME = '22:22:22'
declare #start datetime = cast(#StartDate as datetime) + coalesce(#StartTime, cast('00:00:00' as time))
declare #end datetime = cast(#EndDate as datetime) + coalesce(#EndTime, cast('23:59:59' as time))
select #start, #end
Related
What does the following code do??
Declare #StartDate datetime; -- figure
Declare #EndDate datetime; -- figure
Declare #CurrentDate datetime
Set #CurrentDate = #StartDate
While #CurrentDate <= #EndDate
Begin
Insert Into dbo.Tbl_Time_Dimension
Values (#CurrentDate,
year(#CurrentDate),
datepart(Quarter, #CurrentDate),
month(#CurrentDate),
datepart(week, #CurrentDate),
day(#CurrentDate))
Select #CurrentDate = DateAdd(dd, 1, #CurrentDate)
End
This code will iterate from #StartDate to #EndDate skipping by day and will insert a row into a table called Tbl_Time_Dimension.
The fields being saved into this table are : Date, Year, Quarter, Month, Week and Day for each date in the date range.
Declare #StartDate datetime; --figure
Declare #EndDate datetime; --figure
Declare #CurrentDate datetime
-- Starts the iterator on #StartDate
Set #CurrentDate=#StartDate
-- Iterates until #EndDate
While #CurrentDate<=#EndDate
Begin
-- Saves data in table
Insert Into dbo.Tbl_Time_Dimension
Values (#CurrentDate, year(#CurrentDate),
datepart(Quarter,#CurrentDate),
month(#CurrentDate),
datepart(week,#CurrentDate),
day(#CurrentDate))
-- Increments the date by 1 day
Select #CurrentDate=DateAdd(dd,1,#CurrentDate)
End
this query gets two date
and for each day between startdate and enddate insert that date into Tbl_Time_Dimension as well as it's month , week , Day and quarter
I Have created a date dimension from using the following:
https://www.codeproject.com/Articles/647950/Create-and-Populate-Date-Dimension-for-Data-Wareho
This is just a standard one and I have added 2 columns for OpeningTime and Closing Time with the value of Opening being 08:00:00 and Closing being 18:00:00.
I also have a computed field that works out the difference in minutes between the start time and end time as a non persistent computed field.
I have the following Logic below, but before give you this, let me set the scene of showing the context of the situation of the business, so they are normally open from 8am to 6pm Mon - Fri and also Sat at 8am - 1pm
However there can be exception to the rules as they can open for longer or even on sundays. The Code i have below for some reason falls over by showing less on a sunday given that on rare occasions if they were to be open on a sunday for argument sake 8am to 1pm. Obviously for whatever extending opening hours they are open for even on a Sunday, I would manually have to add it this to the Dim_Date calender which for starttime and enddate I have 00:00:00 for both giving me a 0 for datediff in min by default unless the business tell me otherwise.
But the following Code does NOT take that into account and calculates less than what it should be, something wrong with the code if someone can please provide a solution, basically I just want the code to be flexible so I only make changes to the calender even for extended hours or unusual business hours and the code to reflect that. Thank you.
declare #Date1 datetime = '2017-08-01 08:00:00'
declare #Date2 datetime = '2017-08-07 09:10:00'
declare #StartTime time = cast(#Date1 as time)
declare #EndTime time = cast(#Date2 as time)
declare #CCStartTime time = (select StartTime from dim_date where id = convert(nvarchar(8),#Date1,112))
declare #CCEndTime time = (select EndTime from dim_date where id = convert(nvarchar(8),#Date2,112))
declare #ActualStart time = (select case
when datename(DW,#Date1)='Sunday' then '00:00:00'
when #StartTime between '00:00:00' and #CCStartTime then #CCStartTime
when #StartTime between #CCStartTime and #CCEndTime then #StartTime
when #StartTime between #CCEndTime and '23:59:59' then #CCEndTime end)
declare #ActualEnd time = (select case
when datename(DW,#Date2)='Sunday' then '00:00:00'
when #EndTime between '00:00:00' and #CCStartTime then #CCStartTime
when #EndTime between #CCStartTime and #CCEndTime then #EndTime
when #EndTime between #CCEndTime and '23:59:59' then #CCEndTime end)
declare #DiffrenceStart int = isnull(DATEDIFF(minute,#CCStartTime,#ActualStart),0)
declare #DiffrenceEnd int = isnull(DATEDIFF(minute,#ActualEnd,#CCEndTime),0)
/*
select #StartTime as StartDate
select #EndTime as EndDate
select #CCStartTime as CCStartDate
select #CCEndTime as CCEndDate
select #ActualStart as ActualStart
select #ActualEnd as ActualEnd
select abs(#DiffrenceStart) as DiffrenceStart
select abs(#DiffrenceEnd) as DifrenceEnd
*/
select sum(Min)- (#DiffrenceStart + #DiffrenceEnd)
from dim_date
where id between convert(nvarchar(8),#Date1,112) and convert(nvarchar(8),#Date2,112)
Look at this:
declare #ActualEnd time = (select case
when datename(DW,#Date2)='Sunday' then '00:00:00'
souldn't it be:
declare #ActualEnd time = (select case
when datename(DW,#Date2)='Sunday' then '23:59:59'
I'm writing a report that needs to collect data each day, between 0900hs and 1700hs.
I thought it would be fine as follows:
cast(convert(char(8),t.trxtime,112)as time)
between CONVERT(VARCHAR(5),getdate(),108) >= '09:00'
and CONVERT(VARCHAR(5),getdate(),108) < '17:00'
....BUT no cigar.
Thank you!!!
Hmmm, you could just use datepart():
where datepart(hour, t.trxtime) between 9 and 16 and
cast(t.trxtime as date) = cast(getdate() as date)
I'm not sure if the date comparison is actually necessary.
You could do something like this (assuming you mean actually for the current date, and not for every date in a range:
declare #startDate datetime
declare #endDate datetime
select #startDate = '2014-11-03 09:00:00',
#endDate = '2014-11-03 17:00:00'
select *
from table
where myDate between #startDate and #endDate
if you did mean between 0900 and 1700 for each day, you could do:
declare #startDate datetime
declare #endDate datetime
select #startDate = '2014-10-03',
#endDate = '2014-11-03' -- note i'm still limiting it to a range of ~1 month
select *
from table
where myDate between #startDate and #endDate
and datepart(hour, myDate) between 9 and 17
I am working on requirement where the day starts at 23:00 last day.
I need to get day start date time for passed date to SQL function. We are storing custom day start time (i.e. 23:00) and offset = -1 (means starting last day) in table now.
e.g. If I pass :
10/30/2013 22:00 it should return 10/29/2013 23:00
10/30/2013 23:20 it should return 10/30/2013 23:00
10/31/2013 01:00 it should return 10/30/2013 23:00
Currently I am using following statement : (DayStartOffset = -1 and DayStartTime = 23:00:00)
declare #datetime datetime
set #datetime = '2013-10-30 23:59:59'
declare #date date
declare #time time(3)
set #date = #datetime
set #time = #datetime
declare #dayStartDateTime datetime
--DayStartOffset is set to -1
--DayStartTime set to '23:00'
SELECT #dayStartDateTime = DATEADD(dd,DayStartOffset,CAST(#date AS DATETIME)) + CAST(DayStartTime AS DATETIME)
from table_name
print #dayStartDateTime
But it is not working correctly for e.g. 1 and 3 above
Can you please help me on this function.
In one line
Create Function dbo.MyStartOfDay(#datetime datetime) returns datetime as
begin
return dateadd(hour, -1, dateadd(day, datediff(day, 0, dateadd(hour, 1, #datetime)), 0))
end
Go
Select dbo.MyStartOfDay('2013-10-30 22:00');
Broken down
Create Function dbo.MyStartOfDay(#datetime datetime) returns datetime as
begin
declare #hourLater datetime = dateadd(hour, 1, #datetime)
declare #roundToDay datetime = dateadd(day, datediff(day, 0, #hourLater), 0);
declare #hourBack datetime = dateadd(hour, -1, #roundToDay)
return #hourBack
end
In other words, add an hour on, round down to the nearest day then take an hour off.
Example SQLFiddle
I have a date parameter so the date and time can always change.
For this example the datetime is '2010-07-06 14:46:37.577'
I need to see how much time is between this date paramter and the time of '17:00:00.000'
The time of 5PM will never change but as I said the date paramter can change.
declare #MyDate datetime
set #MyDate = '2010-07-06 14:46:37.577'
select DATEDIFF(MINUTE, #MyDate, CONVERT(varchar(10), #Mydate, 101)+' 17:00:00')
DECLARE #DateParameter datetime
DECLARE #DateTime5PM datetime
SET #DateParameter = '2010-07-06 14:46:37.577'
SET #DateTime5PM = CAST(CONVERT(varchar, #DateParameter, 101) + ' 17:00' AS datetime)
SELECT DATEDIFF (MI, #DateParameter, #DateTime5PM)