add time greater than 24 hours to datetime in sql - sql

I have one datetime field, '2017-05-03 10:00:00' and I want to add the time with field greater than 24 hours, like '30:05' which means 30 hours and 5 minutes in the datetime field. how could I do that? thanks. I'm using sql server 2014.

To get the hours:minutes to minutes:
DATEDIFF(MINUTE, 0, timestr)
Where timestr is your 'h:m' string for input.
The result will be minutes. You should be able to then use this result with the DATEADD function.
DATEADD(minute, DATEDIFF(MINUTE, 0, timestr), dateTimeColumn)
So in an UPDATE I'd expect something like:
SET dateTimeColumn = DATEADD(minute, DATEDIFF(MINUTE, 0, timestr), dateTimeColumn)

Related

Is there a way to add fractions in dateadd

I was surprised to see this query gives two columns with identical values
select
dateadd(hour, -1.5, GETUTCDATE()) as OneAndAHalf,
dateadd(hour, -1, GETUTCDATE()) as One
Is it possible to use dateadd in this way, or am I going to have to calculate the datetime in code?
According to the Docs the answer is no. You cannot add a non-integer value (either as a literal or as an expression) as 'number' in the DATEADD function.
As other members mentioned: switch the DATEPART to an appropriate interval (like minutes) and use that instead. To add "fractions of a second" you could use 'millisecond' which adds thousandth's of a second.
select getdate()
union all
select dateadd(millisecond,100, getdate())
(No column name)
2021-08-06 11:15:04.260
2021-08-06 11:15:04.360
Syntax SQL
DATEADD (datepart , number , date )
number An expression that can resolve to an int that DATEADD adds to a
datepart of date. DATEADD accepts user-defined variable values for
number. DATEADD will truncate a specified number value that has a
decimal fraction. It will not round the number value in this
situation.
Multiply your increment by 60 and add it as minutes (or multiply them by 3600 and add it as seconds)
select
dateadd(minute, -1.5 * 60, GETUTCDATE()) as OneAndAHalf,
dateadd(minute, -1 * 60, GETUTCDATE()) as One
select
dateadd(second, -1.5 * 3600, GETUTCDATE()) as OneAndAHalf,
dateadd(second, -1 * 3600, GETUTCDATE()) as One

Round a timestamp

I would like to differentiate between two dates and round the result to the next day.
For exemple, if I have date1='2020-03-10 11:59:00' and date2='2020-03-10 20:53:00', the difference between date1 and date2 with datediff() is equal to 8 hours. I would like to round this result to have 24 hours.
EDIT
I tried by using dateadd() like this :
select DATEADD(HOUR, DATEDIFF(HOUR, '2020-03-10 11:59:00', '2020-03-10 20:53:00'),0)
Return 1900-01-02 09:00:00.000 doesn't correspond to what I want.
The explanation of the output is not at all clear so I am just guessing here. I am taking the difference in days between two dates. Then adding 1 because if two dates are the same day the difference in days is 0. Then multiplying that result by 24. I changed up the date literal so it is ANSI compliant and will always get the correct date regardless of localization or language settings.
declare #date1 datetime = '20201003 11:59:00'
, #date2 datetime = '20201003 20:53:00'
select datediff(day, #date1, #date2) + 1 * 24

How can I find exact number of minutes without rounding between two dates in sql server

I am new to Sql server, I tried datediff several times but failed. All I need is the exact or precise number of minutes including fraction between two dates. E.g if the gap between two datetime fields is 40 seconds than it gives me 0.67 minutes and not 1 minute. Please help.
Take the difference in seconds and divide by 60.0:
select datediff(second, date1, date2) / 60.0 as diff_in_minutes
This should be close enough for most work. If you really want, you could use milliseconds instead of seconds.
select datediff(millisecond, date1, date2) / 60000.0 as diff_in_minutes
To get MILLISECOND accurate value
DECLARE #D1 DATETIME = '2015-04-16 21:38:02.610'
DECLARE #D2 DATETIME = '2015-04-16 21:38:29.023'
SELECT DATEDIFF(MILLISECOND, #D1, #D2)* 1.000 / ((1.66667) * POWER(10,5))
RESULT: 0.158477683044633910732

SQL Datetime as time query

I would like the below select statement to be show as time rather than datetime. I'm aware that a cast as solution is likely however given I am trying to Cast as a "grouped by time", I am not finding a solution.
SELECT
DATEADD(hour, DATEDIFF(hour, 0,dbo.tbReceiptLine.Time), 0) AS Hourly,
DATEADD(minute, DATEDIFF(minute, 0, dbo.tbReceiptLine.Time) / 30 * 30, 0) AS [30 Mins]
I would like to show this as time only.
In SQL Server 2008 onwards you can cast to time datatype:
SELECT CAST(dbo.tbReceiptLine.Time as time)
See The ultimate guide to the datetime datatypes

DateDiff in SQL Server asking for help

I am using SQL Server 2008. I have a table which has a datetime type column called CreateTime. I need to select all the records from this table whose CreateTime is more than 3 days and half an hour from current time (UTC time). I am using T-SQL store procedure.
BTW: The CreateTime column is some time in the past time.
I have taken quite some time to learn and search for help from MSDN for DateDiff, but cannot figure out. Could anyone show me a sample please?
thanks in advance,
George
You can select and add a WHERE clause with a DATEDIFF using minutes:
SELECT (fields)
FROM (table)
WHERE
DATEDIFF(MINUTE, CREATETIME, getutcdate()) <= (3*24*60 + 30)
And of course, if you only wants those rows which are MORE than 3 days and 30 minutes away, just use the opposite:
WHERE
DATEDIFF(MINUTE, CREATETIME, getutcdate()) > (3*24*60 + 30)
A sample:
SELECT
DATEDIFF(MINUTE, '2009-08-01 08:00:00', getutcdate()),
DATEDIFF(MINUTE, '2009-07-31 20:00:00', getutcdate()),
DATEDIFF(MINUTE, '2009-07-23 20:00:00', getutcdate())
gives as result:
96 816 12337
So the first two dates are still within your 4350 minute bracket (less than 3 days and 30 minutes ago), while the third date is further away.
Marc
You need DATEADD:
WHERE DATEADD(minute, 4350, CreateTime) <= getutcdate()
Or, as you mentioned, you can use DATEDIFF:
WHERE DATEDIFF(minute, CreateTime, getutcdate()) <= 4350
(4350 is '3 days and 30 minutes' in minutes)
One minor quibble with the given answers, though they are correct. Don't apply the function to the column: apply the function to the comparison value.
If CREATETIME is indexed then it's a scan rather than seek with the function on the column.
You don't need millions of rows for this to be a problem.
Adapting the answer of marc_s:
SELECT (fields)
FROM (table)
WHERE
CREATETIME <= DATEADD(MINUTE, - (3*24*60 + 30), getutcdate())