Skip to content
Advertisement

How to query a database table which store the datetime values as biginit

I am working on a third party SQL Server database, and inside the following table they store the RESPONSETIME as bigint as follows:

enter image description here

Now I want to get the records which have their RESPONSETIME between 22-March till 31-March, something as follow:

where srm.RESPONSETIME >= '2021-03-22 00:00:00' and srm.RESPONSETIME <= '2021-03-31 23:59:59'

Currently when I run the above I will get this error:

Error converting data type varchar to bigint.

So is there a way to have things working well for me?

Thanks

Now the 3rd party provide this function to convert the long into the related data:-

USE [Servicedesk]
GO
/****** Object:  UserDefinedFunction [dbo].[longtodate]    Script Date: 03/29/2021 20:15:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[longtodate](@utc BIGINT)
RETURNS DATETIME
AS 
BEGIN
RETURN DATEADD(MILLISECOND, @utc % 1000, DATEADD(SECOND, @utc / 1000, '19700101'))
END  

But I am not sure how I can add this function to my SQL?

Advertisement

Answer

Rather than converting the responsetime to a date to compare, you should do the reverse and convert your date to the same format. This will allow SQL Server to seek directly to the relevant rows using a suitable index on ResponseTime.

Assuming the date is @yourdate and assuming the integer value is milliseconds since 01/01/1970 then the following should hopefully work

Select * from
survey_response_main
where responsetime = cast(Datediff(s, '19700101', @yourdate) as bigint)*1000
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement