Click here to Skip to main content
15,886,085 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i work on sql server 2019 i face issue
when run query to
get queries run on server
i get error

Msg 535, Level 16, State 0, Line 1
The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use datediff with a less precise datepart.


so how to solve this issue please ?

What I have tried:

SELECT
    p.spid,
    RIGHT(CONVERT(varchar, DATEADD(ms, DATEDIFF(ms, p.last_batch, GETDATE()), '1900-01-01'), 121), 12) AS [batch_duration],
    p.[program_name],
    p.hostname,
    MAX(p.loginame) AS loginame,
    (SELECT SUBSTRING(text, COALESCE(NULLIF(spid.stmt_start, 0), 1) + 1, CASE spid.stmt_end WHEN -1 THEN DATALENGTH(text) ELSE (spid.stmt_end - spid.stmt_start) END) FROM ::fn_get_sql(spid.[sql_handle])) AS [sql]
FROM
    master.dbo.sysprocesses p
    LEFT JOIN (
        SELECT
            ROW_NUMBER() OVER(PARTITION BY spid ORDER BY ecid) AS i,
            spid,
            [sql_handle],
            CASE stmt_start WHEN 0 THEN 0 ELSE stmt_start / 2 END AS stmt_start,
            CASE stmt_end WHEN -1 THEN -1 ELSE stmt_end / 2 END AS stmt_end
        FROM sys.sysprocesses
    ) spid ON p.spid = spid.spid AND spid.i = 1
WHERE
    p.spid > 50
    AND p.status NOT IN ('background', 'sleeping')
    AND p.cmd NOT IN ('AWAITING COMMAND', 'MIRROR HANDLER', 'LAZY WRITER', 'CHECKPOINT SLEEP', 'RA MANAGER')
GROUP BY
    p.spid,
    p.last_batch,
    p.[program_name],
    p.hostname,
    spid.stmt_start,
    spid.stmt_end,
    spid.[sql_handle]
ORDER BY
    batch_duration DESC,
    p.spid
Posted
Updated 2-Mar-22 5:31am
Comments
Maciej Los 2-Mar-22 11:20am    
Have you ever done something yourself? At this moment you've asked 359 questions, most with SQL tag.
Patrice T 2-Mar-22 11:33am    
Asking for a all cooked solution is much easier than doing self research. :)
Richard Deeming 2-Mar-22 11:47am    
Agreed. This user is increasingly looking like a "help vampire".
Patrice T 2-Mar-22 11:32am    
What about printing the values of variables involved ?

1 solution

A quick look at the documentation at DATEDIFF (Transact-SQL) - SQL Server | Microsoft Docs[^] would have shown you the answer.
 
Share this answer
 
Comments
Maciej Los 2-Mar-22 13:42pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900