How to check if DateTime lies within String times? - sql

DECLARE #CurDate datetime,
#Begintime nvarchar(10),
#Endtime nvarchar(10)
SELECT #CurDate = GETDATE()
SELECT #Begintime = '9:00 AM'
SELECT #Endtime = '5:00 PM'
What is the best way to check if the CurDate lies within the given times? Thanks!

With SQL Server 2008 and higher, you can convert your current time, and the begin and end times (as nvarchar) into TIME variables and then do the check:
DECLARE #CurrTime TIME = CAST(#CurDate AS TIME)
DECLARE #TimeFrom TIME = CAST(#Begintime AS TIME)
DECLARE #TimeTo TIME = CAST(#Endtime AS TIME)
SELECT
#CurrTime, #TimeFrom, #TimeTo,
CASE
WHEN #CurrTime BETWEEN #TimeFrom AND #TimeTo THEN 'Yes!'
ELSE 'No, sorry'
END

Use the function DATEDIFF to calculate the hours and minutes difference.
Here -> https://msdn.microsoft.com/en-us/library/ms189794.aspx
I wouldn't use use nvarchar, but instead of it use something like this:
#BeginTime = DATEADD(hour, 9, CONVERT(datetime,CONVERT(date,getdate())))
#EndTime = DATEADD(hour, 17, CONVERT(datetime,CONVERT(date,getdate())))
The condition to use can be:
WHERE #CurDate BETWEEN #BeginTime AND #EndTime
Best regards,

Related

How to compare time in 24 hours format in sql server

I want to compare two time values with current time in sql server. The time is in 24 hours format. The code I am trying is,
declare #StartTime varchar(10)='16:30'
declare #EndTime varchar(10)='10:10'
declare #CurrTime varchar(10)='09:30'
select case when CONVERT(time,#CurrTime) between CONVERT(time,#StartTime) and CONVERT(time,#EndTime) then 'SUCCESS' else 'FAIL' end
and gives the output as 'FAIL'.
Please suggest solution.
You need to use datetime datatype and CASE WHEN to check if start date > end date to apply DATEADD to star date in case it is bigger then end date:
DECLARE #Start nvarchar(5)= N'16:30',
#End nvarchar(5)= N'10:10',
#Curr nvarchar(5)=N'09:30'
DECLARE #curdate nvarchar(9) = CONVERT(nvarchar(10),GETDATE(),112)+' '
DECLARE #starttime datetime = #curdate + #Start + ':00.000',
#endtime datetime = #curdate + #End + ':00.000',
#currtime datetime = #curdate + #Curr + ':00.000'
SELECT CASE WHEN #currtime between (
CASE WHEN #starttime > #endtime
THEN DATEADD(day,-1,#starttime)
ELSE #starttime END
) and #endtime THEN 'SUCCESS'
ELSE 'FAIL' END as result

SQL Separate hours in Start and EndDate

I'm need a help to create a Query. My problem is I have a StartDate and EndDate and need separate this in blocs of 60 minutes.
DECLARE #STARTDATE AS SMALLDATETIME
DECLARE #ENDDATE AS SMALLDATETIME
SET #STARTDATE = '2012-11-21 11:03:00'
SET #ENDDATE = '2012-11-21 13:04:00'
I need the return:
Hour, Time
11 , 57
12 , 60
13 , 04
You could use a recursive CTE. For example:
declare #startDate datetime = '2012-11-21 22:05:00'
declare #endDate datetime = '2012-11-22 01:06:00'
; with TimeList as
(
select #startDate as dt
union all
select dateadd(hour, 1, dateadd(hour, datediff(hour, 0, dt), 0))
from TimeList
where dateadd(hour, 1, dt) < #endDate
)
select dt
from TimeList
union all
select #endDate
The snippet dateadd(hour, datediff(hour, 0, dt), 0) removes the hours and minutes from a date. It does so by calculating the number of hours since date 0 and then adding that number of hours to date 0.
Live example at SQL Fiddle.
I unsure if i understood you but this will return the hour and minute after your start date at 60 min intervals.
DECLARE #STARTDATE AS SMALLDATETIME
DECLARE #ENDDATE AS SMALLDATETIME
DECLARE #time AS TABLE(id int identity(1,1), [hour] int, [time] int)
SET #STARTDATE = '2012-11-21 11:03:00'
SET #ENDDATE = '2012-11-21 13:04:00'
WHILE #STARTDATE < #ENDDATE
BEGIN
SELECT #STARTDATE = DATEADD(MINUTE,60,#STARTDATE)
INSERT INTO #time (hour,time)
VALUES(DATEPART(HOUR,#STARTDATE),DATEPART(MINUTE,#STARTDATE))
END
SELECT * FROM #time
You coan do it in three pieces. First piece is for the first hour, 60 minus the minute value, 2nd piece is time=60 for all hours between start+1 and end, third piece is end minutes
and then insert them into a temp table, as abstractChaos has done.
Insert into temp table like AbstractChaos:
DECLARE #STARTDATE AS SMALLDATETIME
DECLARE #ENDDATE AS SMALLDATETIME
DECLARE #TIME AS TABLE(id INT IDENTITY(1,1), [HOUR] INT, [TIME] INT)
SET #STARTDATE = '2012-11-21 11:03:00'
SET #ENDDATE = '2012-11-21 13:04:00'
INSERT INTO #TIME (HOUR,TIME)
VALUES (datepart(HOUR,#startdate) ,60 - datepart(MINUTE,#startdate) )
WHILE #STARTDATE < #ENDDATE
BEGIN
SELECT #STARTDATE = DATEADD(MINUTE,60,#STARTDATE)
INSERT INTO #TIME (HOUR,TIME)
VALUES(datepart(HOUR,#STARTDATE) , 60)
END
INSERT INTO #TIME (HOUR,TIME)
VALUES(datepart(HOUR,#enddate) , datepart(MINUTE,#startdate))

TSQL strip date from datetime

What is the best way to strip the date from a DATETIME so only time is left to do a comparison?
I know I can do the following:
CONVERT(DATETIME, CONVERT(VARCHAR(8), GETDATE(),8))
But this involves convert and characters. If I wanted to check whether a time (including minutes) was between two other times stored in DATETIME columns, is there an elegant way to do this without having to rely on converting to character strings?
If you're using SQL 2008
SELECT CAST(GETDATE() AS TIME)
Try the TimeOnly function from Essential SQL Server Date, Time, and DateTime Functions:
create function TimeOnly(#DateTime DateTime)
returns datetime
as
begin
return dateadd(day, -datediff(day, 0, #datetime), #datetime)
end
go
Or simply cast the DateTime as Time in SQL Server 2008:
declare #time as time
set #time = cast(dateTimeVal as time)
DECLARE
#Now DateTime,
#Today DateTime,
#Time DateTime
SET #Now = GetDate()
SET #Today = DateAdd(dd, DateDiff(dd, 0, #Now), 0)
SET #Time = DateAdd(ss, DateDiff(ss, #Today, #Now), 0)
SELECT #Time
Just another way to get date and time from datetime.
datetime in SQL Server implemented as float value where whole part floor(value) is day from 1900-01-01 and fractional part (value - floor(value)) is part of twenty-four hours elapsed from start of day
select
d as [datetime],
cast(cast(T.d as float) - floor(cast(T.d as float)) as datetime) as [time],
cast(floor(cast(T.d as float)) as datetime) as [date]
from
(values
(getdate()),
('1753-01-01 23:59:59.700'),
('1899-12-31 00:00:00.300'),
('1900-01-01 00:00:00.300')
) as T(d);

SQL need to subtract certain time from date parameter passed into query

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)

To get difference between 2 dates

I have 2 dates. I want to get number of days between 2 dates in storedprocedure.
DateDiff function should do what you need
declare #var1 Datetime
declare #var2 Datetime
set #var1 = '2009-04-01'
set #var2 = '2009-04-16'
SELECT datediff(day,#var1, #var2 )
How about using the dateDiff function ?
eg
DECLARE #Dt INT
SET #Dt = DATEDIFF(dd,#StartDate,#EndDate)
should do the trick ?
Or did I miss something ?
USE tempdb
DECLARE #DATE1 datetime
DECLARE #DATE2 datetime
SET #DATE1 = '01/01/2000'
SET #DATE2 = '02/01/2000'
SELECT DATEDIFF(day, #DATE1, #DATE2)
DATEDIFF is the way to do it
Note that DATEDIFF only concerns itself with the date portion. If times are involved, a converted subtraction might yield better results.
DECLARE #start DATETIME
DECLARE #end DATETIME
SET #start = '20090514 00:00:00'
SET #end = '20090514 23:59:59'
PRINT CONVERT(FLOAT, (#end-#start)) -- 0.999988
PRINT DATEDIFF(DAY,#start,#end) -- 0