Reputation: 35
I have a float datatype that is actually a date. I need to use it in the condition to obtain the data for the last 10 minutes.
I used CAST
to convert from float to datetime. and then DATEADD
to collect the last 10 minutes data but its not working.
select CAST(StartTime AS DATETIME) as StartTime
,CAST(endtime AS DATETIME) as EndTime
from BIORADFM_TASK_AUDIT
where CAST(StartTime AS DATETIME) >= DATEADD(minute, 10, GETDATE());
Result:
StartTime EndTime
----------------------- -----------------------
2017-10-12 16:57:06.997 2017-10-12 16:57:06.997
2017-10-12 06:06:59.997 2017-10-12 06:06:59.997
2017-10-12 06:06:47.997 2017-10-12 06:06:47.997
2017-10-11 16:04:53.000 2017-10-11 16:04:53.000
It´s showing data for the next two days, instead of the last 10 minutes starting from getdate.
Table Structure:
CREATE TABLE [dbo].[BIORADFM_TASK_AUDIT](
[STRGUID] [nvarchar](32) NOT NULL,
[ACTIVITYUSERID] [int] NOT NULL,
[ACTIVITYSESSIONID] [int] NOT NULL,
[ACTIVITYCODE] [int] NOT NULL,
[SERVERNAME] [nvarchar](256) NOT NULL,
[APPNAME] [nvarchar](20) NOT NULL,
[STARTTIME] [float] NOT NULL,
[ENDTIME] [float] NOT NULL,
[STRDESCRIPTION] [nvarchar](1000) NULL,
[STRMODULENAME] [nvarchar](300) NULL,
CONSTRAINT [PK_BIORADFM_TASK_AUDIT] PRIMARY KEY CLUSTERED
Sample Data:
StartTime Endtime
43020.2549421296 43020.2549421296
43020.2550810185 43020.2550810185
43020.6342939815 43020.6342939815
43020.2548032407 43020.2548032407
43020.2548263889 43020.2548263889
43020.2549421296 43020.2549421296
43020.2549305556 43020.2549305556
43020.2549421296 43020.2549421296
43019.2549189815 43019.2549189815
Upvotes: 0
Views: 1519
Reputation: 2670
If you want rows with an start time situated only in the last 10 minutes you have to filter by range, for example with BETWEEN
:
SELECT CAST(StartTime AS DATETIME) AS StartTime, CAST(endtime AS DATETIME) AS EndTime
FROM BIORADFM_TASK_AUDIT
WHERE CAST(StartTime AS DATETIME) BETWEEN DATEADD(minute,-10,GETDATE()) AND GETDATE()
Upvotes: 0
Reputation: 1269513
Don't use datediff()
. It counts the number of "time boundaries" between two date/times.
Instead, use date arithmetic:
WHERE A.ActivityUserID = B.lUserID and
CAST(A.StartTime AS DATETIME) >= DATEADD(minute, -10, GETDATE())
Upvotes: 1
Reputation: 33571
Just a wild guess since we have nothing to work with. It would be a good idea to store datetime data as datetime instead of a strings so you don't have to constantly convert your data to the right datatype.
SELECT A.strGUID
, u.sUserName
, A.ServerName
, A.AppName
, A.ActivityCode
, CAST(A.StartTime AS DATETIME) as StartTime
, CAST(A.endtime AS DATETIME) as EndTime
, A.strModuleName
, A.strDescription
, GETDATE()
FROM INTRAWPROD_TASK_AUDIT A
join HSV_ACTIVITY_USERS u on A.ActivityUserID = u.lUserID
where convert(datetime, a.StartTime) >= convert(datetime, dateadd(minute, -10, GETDATE()))
Upvotes: 0