SQL Server: datediff function resulted in an overflow when using MILLISECOND - sql-server

I have the following query :
select CONVERT(varchar(12), DATEADD(MILLISECOND, DateDiff(MILLISECOND, '2014-08-04 10:37:28.713','2014-11-04 08:21:17.723'), 0), 114)
When I execute this, I get the error :
"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."
When I change the query to the following it works fine :
select CONVERT(varchar(12), DATEADD(SECOND, DateDiff(SECOND, '2014-08-04 10:37:28.713','2014-11-04 08:21:17.723'), 0), 114)
The problem is that I really need the MILLISECONDS as well.

A bit later response but may help.
In SQL 2016 MS introduced function DATEDIFF_BIG which will (according to type size) overflow in difference bigger than something like 290k years. But technet article have same time difference as basic DATEDIFF - https://msdn.microsoft.com/en-us/library/mt628058.aspx

See https://learn.microsoft.com/en-us/sql/t-sql/functions/datediff-transact-sql?view=sql-server-ver15#return-value
For millisecond, the maximum difference between startdate and enddate is 24 days, 20 hours, 31 minutes and 23.647 seconds.
If you need millisecond above that level, you'll need to write something custom.

In SQL Server 2016 there is a new function available: DATEDIFF_BIG
It solves exactly the overflow problem.

You don't need to refer to the miliseconds in your calculation.
This will do exactly the same as your script except the overflow:
SELECT CONVERT(varchar(12),
CAST('2014-11-04 08:21:17.723' as datetime) -
CAST('2014-08-04 10:37:28.713' as datetime)
, 114)

For me there was a big interval between two dates so i have used below code
declare #timetagInMillsecond bigint=CAST(CAST( cast(#timetag as
datetime) -'1970-01-01' AS decimal(38,10))*24*60*60*1000+0.5 as
bigint)
It works for me .

Use DATEDIFF_BIG to resolve the overflow issue
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.
SELECT DATEDIFF_BIG(
millisecond,
SYSDATETIME(),
DATEADD(year, 1000, SYSDATETIME()) ) AS 'Milliseconds in 1000 years';

For SQL Server 2014, the following works around the 'int' limitation to obtain a "JavaScript Time Epoch". This assumes the start epoch is itself a date, which fits the local use-case leading to finding this question. The query requires adaptation to the specific question use-case which does not have this property.
declare #x as datetime = getdate()
-- epoch_delta_s_to_date * 1000 + day_delta_ms
select
cast(datediff(second, '1970-01-01', cast(#x as date)) as bigint) * 1000
+ cast(datediff(millisecond, cast(#x as date), #x) as bigint)
For the case of obtaining a "JavaScript Time Epoch" this is still subject to the Y2038 limitation of datediff(second, '1970-01-01', ..).

Related

slice data between the range of dates SQL Server

I am having a miserable time in achieving a simple task. I want to slice data for every 6-months every month. I am not getting any output. My SQL skills are very bad. I searched for the solution a lot and they gave me some idea but I am not able to any output.
Below is my attempt:
SELECT TOP 10000 CONVERT(VARCHAR,DATEADD(HOUR,-4,DATEADD(s, ch.dateTimeOrigination, '19700101')), 121) as CallDate
FROM [dbo].[CallData] AS ch
WHERE LEN(ch.callingPartyNumber) = 4 AND
CONVERT(VARCHAR,DATEADD(HOUR,-4,DATEADD(s, ch.dateTimeOrigination, '19700101')), 121) BETWEEN CONVERT(VARCHAR(10), GETDATE(), 110) AND CONVERT(VARCHAR(10), DATEADD(month, -6, GETDATE()), 110)
The table definitely has the data for the time period I am trying to query. So I am not sure why this is not giving me any output. I will really appreciate your help. Thank you.
I believe your BETWEEN values are backwards.
EDIT: Your CONVERT statements in the WHERE clause are also different formats, so when you compare your varchars, the comparison fails. You should instead use DATETIME.
Try this:
SELECT TOP 10000
CONVERT(VARCHAR,DATEADD(HOUR,-4,DATEADD(s, ch.dateTimeOrigination, '19700101')), 121) as CallDate
FROM [dbo].[CallData] AS ch
WHERE LEN(ch.callingPartyNumber) = 4 AND
CAST(DATEADD(HOUR,-4,DATEADD(s, ch.dateTimeOrigination, '19700101')) AS DATETIME) BETWEEN DATEADD(month, -6, GETDATE()) AND GETDATE()

DATEDIFF check for one minute is actually between one and two minutes

I am using DATEDIFF to make sure a certain action cannot happen more than once a minute.
SELECT COUNT(*)
FROM FOOD_ACTION
WHERE
DATEDIFF(MINUTE,INSERT_DT,GETDATE()) <= 1
INSERT_DT is a date column with the same format that GETDATE() returns
If anything is returned from this query, the action is not allowed to happen.
The thing is, it only kind of works. I've had it range from a minute and 10 seconds all the way up to almost two minutes before it will let the action happen again. There doesn't seem to be any consistency in if its closer to one minute or two, and I've confirmed that GETDATE() returns the same exact time as my application does when it sets the INSERT_DT column.
What's up with the inconsistent and incorrect timing?
When you do a datediff, T-SQL will look for when the time/date part boundaries have crossed to increment the difference.
So with:
DECLARE #StartDate DATETIME = '2000-01-01T12:51:59.999',
#EndDate DATETIME = '2000-01-01T12:52:00:000'
SELECT DATEDIFF(minute, #StartDate, #EndDate)
you will get a result of: 1
even though there was only 1ms difference between the dates.
In practice I solve this issue by going to a lower granularity and look for the cross over myself, ie:
DECLARE #StartDate DATETIME = '2000-01-01T12:51:59.999',
#EndDate DATETIME = '2000-01-01T12:52:00:000'
SELECT
CASE WHEN DATEDIFF(second, #StartDate, #EndDate) >= 59 THEN CAST(1 as bit)
ELSE CAST(0 as bit)
END as InNewMinute
That'll give 0 as obviously 1ms is not 1 minutes difference.
Note: the code above is obviously only for handling 1 minute difference
See: MSDN DATEDIFF (Transact-SQL) (look at datePart Boundaries)
SELECT COUNT(*)
FROM FOOD_ACTION
WHERE
DATEDIFF(MILLISECOND,INSERT_DT,GETDATE()) <= 60000
also why access all data to indicate nothing should happen? Better to do
SELECT 1 as marker
WHERE
DATEDIFF(MILLISECOND,INSERT_DT,GETDATE()) <= 60000
then if 1 is returned, use that as an indication, that way read no data and cut network traffic possibly

Query epoch time using SQL Server to find date range

I have to query an SQL Server database and the table's values use Epoch time (an int. Here's an example - 1438005018). I am wondering how I can write a query so that I can say the following...
select
*
from
tablename
where
epochdate between 'yesterday at 12:00' and 'today at 12:00' --this is the part I'm not sure about.
Ideally, if it's easy, I'd like the query to use non-epoch logic as Epoch time confuses the crap out of me. Maybe there's a quick way of converting in SQL Server?
I posted a link above in the comments that may be a more practical solution if you're able to deploy functions in the database you're working with, but if you're only able to query, this is an option to try as well (this assumes SQL Server 2008 and above):
declare #todayepoch bigint, #yesterdayepoch bigint;
select #todayepoch =
cast((cast(dateadd(hour, 12,
cast(cast(sysutcdatetime() as date) as datetime)) as decimal(24,10))
- cast(cast('1970-01-01' as datetime) as decimal(24,10)))
*60.0*60.0*24.0 as int), -- + 18000, --Eastern time
#yesterdayepoch =
cast((cast(dateadd(hour, -12,
cast(cast(sysutcdatetime() as date) as datetime)) as decimal(24,10))
- cast(cast('1970-01-01' as datetime) as decimal(24,10)))
*60.0*60.0*24.0 as int) -- + 18000 --Eastern time
select #todayepoch, #yesterdayepoch
select
*
from
tablename
where
epochdate between #yesterdayepoch and #todayepoch
I used UTC above as a presumption of comparing based on UTC times, but you could also compare to your time zone, with the appropriate addition/subtraction of your time zone difference in seconds (e.g., add 18000 to each variable to get noon in Eastern Standard Time).
You can test your results by using http://www.epochconverter.com/ to compare your values in your variables.
You query would look like the following:
DECLARE #dt_from DATETIME;
DECLARE #dt_to DATETIME;
SELECT
#dt_from=DATEADD(HH,-12,CAST(FLOOR(CAST(GETUTCDATE() AS FLOAT)) AS DATETIME)), -- strip time of current UTC date/time, and subtract 12 hrs
#dt_to=DATEADD(HH,+12,CAST(FLOOR(CAST(GETUTCDATE() AS FLOAT)) AS DATETIME)); -- strip time of current UTC date/time, and add 12 hrs
SELECT
*
FROM
tablename
WHERE
epochdate BETWEEN DATEDIFF(s,'1970-01-01',#dt_from) AND DATEDIFF(s,'1970-01-01',#dt_to);

Getting today's midnight time as UTC

I have the following query which calculates today's midnight value (UTC) as a datetime:
SELECT CONVERT(DATE,GETDATE())+(GETDATE()-GETUTCDATE())
Result: 2011-11-03 19:00:00.000 (for GMT-5 on Nov. 4, 2011)
Not only that, but on occasion, it returns values like these:
2011-11-03 19:00:00.003
2011-11-03 19:00:00.007
2011-11-03 19:00:00.010
..., which are wrong!
There must be a better way to do this.
I already answered this with a solution using DATEADD and DATEDIFF with GETDATE() and GETUTCDATE(), similar to the example given in the original question, but since then I've discovered the datetimeoffset data type added in SQL Server 2008. This stores a datetime along with a timezone offset.
How you use this type will depend on whether you want to change the data type of your existing data. If you don't want to change anything, the following statement will return a datetime type with the local time of midnight:
SELECT CONVERT(datetime, SWITCHOFFSET(CONVERT(datetimeoffset,
CONVERT(date, GETDATE())),
DATENAME(TzOffset, SYSDATETIMEOFFSET())))
You could also convert any UTC time into local time using:
SELECT CONVERT(datetime, SWITCHOFFSET(CONVERT(datetimeoffset,
#myutctime,
DATENAME(TzOffset, SYSDATETIMEOFFSET())))
The datetimeoffset type is only available using SQL2008 and above. If you need to do this with 2005 and below, you can use a solution similar to the one in the original question, but altered to account for the fact that GETDATE() - GETUTCDATE() is not an atomic operation and will likely involve milliseconds of difference between when the two are executed.
SELECT DATEADD(minute,
DATEDIFF(minute, GETUTCDATE(), GETDATE()),
CONVERT(datetime, CONVERT(date, GETDATE())))
This will take the number minutes between GETDATE() and GETUTCDATE() and add them onto the local midnight time. Unfortunately, you have to convert back from date to datetime as DATEADD won't work with minutes if you give it a date. I'd suggest wrapping this into a user-defined function to make it look less verbose, e.g.
CREATE FUNCTION dbo.MidnightASUTC(#dt as datetime)
RETURNS datetime
AS
BEGIN
RETURN DATEADD(minute,
DATEDIFF(minute, GETUTCDATE(), GETDATE()),
CONVERT(datetime, CONVERT(date, #dt)))
END
SELECT dbo.MidnightAsUTC(GETDATE())
For a specific scenario like the one you've described ("today's midnight value (UTC) as a datetime"), a programmatic approach makes sense, but if you ever need to extend it to a different question (what was midnight UTC for this summer?), you may want to use a calendar table (to account for things like daylight savings time, etc).

Need only Date from DateTime

I have a variable of DateTime type in SQL.
Just need to have Date part of it.
please Help?
SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
The result is: “2009-07-14 00:00:00.000”
Edit: guess the next variant is more common:
SELECT DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0)
because of the day pattern can be easily changed to week or month pattern. It is very useful when the GROUP BY clause should group by week of month (reports).
This has been asked and answered before on Stack Overflow. In fact, it's been asked over and over:
Most efficient way in MS SQL to get date from date+time?
Best way to check for current date in where clause of sql query.
SQL Drop Time in DateTime
MS SQL Date Only Without Time
How to return the date part only from a SQL Server datetime datatype
Found this using Google
SELECT CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, GETDATE())))
If you just need a varchar representation of the date, you can use the convert function, e.g.
select convert(varchar, getDate(), 102) /* 2009.07.14 */
If you need a datetime (midnight on the given date), you can just convert it back.
select convert(datetime, convert(varchar, getDate(), 102), 102)
-- Sneaky CAST/DATEDIFF trick strips off the time to get just the day (midnight)!
CAST(DATEDIFF(d,0,DateField) AS DATETIME) AS DayField
SQL Server 2008 has a date datatype that stores just the date, if you are inthis version, perhaps this would be a better datat type for you to use. Be warned though, Date doesn't work exactly like datetime for data manipulation.
SELECT DATEADD(day, DATEDIFF(day, '19900101', CURRENT_TIMESTAMP), '19900101')
A very useful article:
"The purpose of this article is to explain how the datetime types work in SQL Server, including common pitfalls and general recommendations."The ultimate guide to the datetime datatypes
Note that converting to varchar and back (convert(datetime, convert(varchar, getDate(), 102), 102)) is much slower.
If you want the format 'MM/DD/YY', use "CONVERT(varchar, #datetimevalue, 1) to display just the date. If you need it in datetime format, use "CONVERT(datetime, CONVERT(varchar, #datetimevalue, 1))".
I created an entry in my SQL blog about how to retrieve and display all possible formats of the CONVERT(varchar, ..) function:
http://jessesql.blogspot.com/2009/04/converting-datetime-values-to-varchar.html
A tip:
If you find yourself doing this often, you can create a scalar User Defined Function containing the time-stripping logic of your choice.
Be warned: SQL Server 2000 has some painful bugs involving UDF's in ON clauses.
datepart(day, datetimevalue)

Resources