简体   繁体   中英

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:

在此处输入图像描述

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?

This looks like the number of milliseconds since 1970-01-01 (one way of measuring the Unix epoch). You can test this by checking the value of:

select dateadd(second, responsetime / 1000, '1970-01-01')

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM