Related
In SQL server 2008, I would like to get datetime column rounded to nearest hour and nearest minute preferably with existing functions in 2008.
For this column value 2007-09-22 15:07:38.850, the output will look like:
2007-09-22 15:08 -- nearest minute
2007-09-22 15 -- nearest hour
declare #dt datetime
set #dt = '09-22-2007 15:07:38.850'
select dateadd(mi, datediff(mi, 0, #dt), 0)
select dateadd(hour, datediff(hour, 0, #dt), 0)
will return
2007-09-22 15:07:00.000
2007-09-22 15:00:00.000
The above just truncates the seconds and minutes, producing the results asked for in the question. As #OMG Ponies pointed out, if you want to round up/down, then you can add half a minute or half an hour respectively, then truncate:
select dateadd(mi, datediff(mi, 0, dateadd(s, 30, #dt)), 0)
select dateadd(hour, datediff(hour, 0, dateadd(mi, 30, #dt)), 0)
and you'll get:
2007-09-22 15:08:00.000
2007-09-22 15:00:00.000
Before the date data type was added in SQL Server 2008, I would use the above method to truncate the time portion from a datetime to get only the date. The idea is to determine the number of days between the datetime in question and a fixed point in time (0, which implicitly casts to 1900-01-01 00:00:00.000):
declare #days int
set #days = datediff(day, 0, #dt)
and then add that number of days to the fixed point in time, which gives you the original date with the time set to 00:00:00.000:
select dateadd(day, #days, 0)
or more succinctly:
select dateadd(day, datediff(day, 0, #dt), 0)
Using a different datepart (e.g. hour, mi) will work accordingly.
"Rounded" down as in your example. This will return a varchar value of the date.
DECLARE #date As DateTime2
SET #date = '2007-09-22 15:07:38.850'
SELECT CONVERT(VARCHAR(16), #date, 120) --2007-09-22 15:07
SELECT CONVERT(VARCHAR(13), #date, 120) --2007-09-22 15
I realize this question is ancient and there is an accepted and an alternate answer. I also realize that my answer will only answer half of the question, but for anyone wanting to round to the nearest minute and still have a datetime compatible value using only a single function:
CAST(YourValueHere as smalldatetime);
For hours or seconds, use Jeff Ogata's answer (the accepted answer) above.
Select convert(char(8), DATEADD(MINUTE, DATEDIFF(MINUTE, 0, getdate), 0), 108) as Time
will round down seconds to 00
DECLARE #PreviousMonthStart DATETIME
DECLARE #PreviousMonthEnd DATETIME
SET #PreviousMonthStart = DATEADD(m,DATEDIFF(m,0,GETDATE())-1,0)
SET #PreviousMonthEnd = DATEADD(ms,-2,DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0))
PRINT #PreviousMonthStart
PRINT #PreviousMonthEnd
when executed above query the result set is in the below format
Mar 1 2017 12:00AM
Mar 31 2017 11:59PM
But I need result set in below format
2017-04-01 00:00:00.000
any help
The result of the t-sql is what you want but because you are using PRINT to see the results it is casting the result into varchar before printing it.
If you simply do a select instead of the PRINT it will show you what you want to see.
DECLARE #PreviousMonthStart DATETIME
DECLARE #PreviousMonthEnd DATETIME
SET #PreviousMonthStart = DATEADD(m,DATEDIFF(m,0,GETDATE())-1,0)
SET #PreviousMonthEnd = DATEADD(ms,-2,DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0))
SELECT #PreviousMonthStart -- Result: 2017-03-01 00:00:00.000
SELECT #PreviousMonthEnd -- Result: 2017-03-31 23:59:59.997
PRINT #PreviousMonthStart -- Result: Mar 1 2017 12:00AM
PRINT #PreviousMonthEnd -- Result: Mar 31 2017 11:59PM
You can add CONVERT with the declared variable with the print statement.
DECLARE #PreviousMonthStart DATETIME
DECLARE #PreviousMonthEnd DATETIME
SET #PreviousMonthStart = DATEADD(m,DATEDIFF(m,0,GETDATE())-1,0)
SET #PreviousMonthEnd = DATEADD(ms,-2,DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0))
PRINT CONVERT(VARCHAR(25),#PreviousMonthStart,120)
PRINT CONVERT(VARCHAR(25),#PreviousMonthEnd,120)
Output:
2017-03-01 00:00:00
2017-03-31 23:59:59
I need to set a where condition to the last date of previous year.
I looking for the solution using dateadd function but i can not figure it out. This code gives me the last day of current month. But how to get the last day of december last year '2015-12-31' of have tried different ways but it all gives me odd results.
declare #today datetime
select #today=getdate()
select convert(varchar(10), dateadd(dd, -day(dateadd(mm, 1, #today)),dateadd(mm, 1, #today)),101)
Assuming Sybase ASE, not ASA or IQ:
declare #lastyear smallint, #dec31 datetime
select #lastyear = datepart(year,getdate()) - 1
select #dec31 = convert(datetime, convert(char(4), #lastyear) + "1231")
select #dec31
This produces
--------------------------
Dec 31 2015 12:00AM
#lastyear contains 2015, the last year, then text "2015" is concatenaded into "20151231", which is the desired date in AAAAMMDD char format. Next step converts it into date, datetime or smalldatetime. #dec31 stores that result.
select convert(datetime, convert(varchar, datepart(year, getdate()) - 1 ) + '/12' + '/31')
Dec 31 2015 12:00AM
SELECT DATEADD(DD,-1,DATEADD(YEAR, DATEDIFF(YEAR, '', GETDATE())+1, ''))
I'm working on a SQL query which returns a integer which is the number of minutes between two given dates as follows
DATEDIFF(mi, date_one, getdate())
The above query returns difference in two dates in minutes but for getdate() I would want to supply my own time.
For example, consider
date_one= 2015-12-29 13:39:03.000
getdate() return current date and time ie., 2015-12-29 14:33:50.000
But, I want to change time part in getdate() to some 10:00:00.00 so that the getdate() is 2015-12-29 10:00:00.00 by passing an hour integer say 10.
May I know a good way to do that?
This will use getDate, but let you set your own hour. Just replace that second parameter (which is 10 with whichever hour you want). Use this expression in place of getDate() in your dateDiff function.
DATEADD(hh, 10, DATEADD(d, DATEDIFF(d, 0, getDate()), 0))
You can also add minutes, seconds, milliseconds, etc. to get what you need.
Here I am adding 633 minutes to make it 10:33 (change the first parameter to mi for minutes).
select DATEADD(mi, 633, DATEADD(d, DATEDIFF(d, 0, getDate()), 0))
See the documentation for other value for the first parameter: https://msdn.microsoft.com/en-us/library/ms186819.aspx
Here is how to use it:
DATEDIFF(mi, getDate(),
DATEADD(mi, 633, DATEADD(d, DATEDIFF(d, 0, getDate()), 0))
)
This will give you the minutes from the current time to 10:33 on the current day. Here is a sqlfiddle: http://sqlfiddle.com/#!6/9eecb7/5407
I find this function useful:
CREATE FUNCTION [dbo].[StripTimeFromDateTime]
(
#date DateTime
)
RETURNS DateTime
AS
BEGIN
RETURN DATEADD(dd, DATEDIFF(dd, 0, #date), 0)
END
This will knock the time off a datetime leaving it at 00:00:00.000. Then you can:
SELECT DATEADD(hour, 10, dbo.StripTimeFromDateTime(GetDate()))
Notice the example below:
select
cast('2015-12-28 12:15:00' as datetime),
getdate(),
cast(cast(convert(date, getdate()) as varchar(20)) + ' 10:00:00' as datetime);
|----------------------------|----------------------------|----------------------------|
| December, 28 2015 12:15:00 | December, 29 2015 20:42:35 | December, 29 2015 10:00:00 |
An example like the one you used:
with example as (
select cast('2015-12-28 12:15:00' as datetime) as date_one
)
select
date_one,
cast(cast(convert(date, getdate()) as varchar(20)) + ' 10:00:00' as datetime) as myown,
datediff(
mi,
date_one,
cast(cast(convert(date, getdate()) as varchar(20)) + ' 10:00:00' as datetime)
) as minutes
from example;
Result:
| date_one | myown | minutes |
|----------------------------|----------------------------|---------|
| December, 28 2015 12:15:00 | December, 29 2015 10:00:00 | 1305 |
Example on SQLFiddle: http://sqlfiddle.com/#!3/9eecb7/6599
The reason I used varchar is to have flexibility of typing a time such as '10:15:00' or other variations of time.
This one-liner will gives the current date with the time part replaced with the constant you want
select cast(cast(getdate() as date) as datetime) + cast(cast('10:00:00' as time) as datetime)
How this works:
Cast the getdate() result to date and then back to datetime to get the current date without the time.
select cast(cast(getdate() as date) as datetime)
Cast '10:00:00' to time and then to datetime to get 10:00:00 as datetime.
select cast(cast('10:00:00' as time) as datetime)
Add the two
select cast(cast(getdate() as date) as datetime) + cast(cast('10:00:00' as time) as datetime)
That's all
select DATEADD (hh,10, CONVERT(Datetime, CONVERT (date, GETDATE())))
First remove time and then add 10 hours.
I want to do a mssql date range to return the data for an hour time range. Meaning, I want to return the data for the last hour, but not for the last hour from the current time.
Declare #today datetime
set #today=GETDATE()
select * from table1 where
datetime>= DATEADD(hh,-2,#Today)
For example, the current time is 11:50:00 a.m. I would like the query to return all the data between 10:00:00 a.m. to 10:59:00 a.m. My variables cannot be static. I would like it dynamic, so no matter what time of the day I run the query it only return the last hour data no matter what time is it now. So it could be any time between 11:00:00 a.m. until 11:59:00 a.m., I still want the result to return data from 10 a.m until 10:59 a.m.
Thank you
Here's a slightly simpler way:
DECLARE #d SMALLDATETIME;
SELECT #d = DATEADD(HOUR, DATEDIFF(HOUR, '20000101', GETDATE()) - 1, '20000101');
SELECT #d;
Now you can use #d in your query, e.g.
WHERE col >= #d AND col < DATEADD(HOUR, 1, #d);
This is an open-ended date range. Please don't think about the "end" of the range or consider this a BETWEEN query. BETWEEN 10:00 AND 10:59 is not a very wise approach, because you may miss data from 10:59:00.003 -> 10:59:59.997. Background info on why BETWEEN is evil.
Try this one :
declare #lowerRange datetime =
dateadd(hh,datepart(hour,dateadd(hh,-1,getdate())) ,
cast(cast(getdate() as date) as smalldatetime) )
declare #upperRange datetime = dateadd(hour,1,#lowerRange)
select * from yourtable where yourdate between #lowerRange and #upperRange
This returns an hour range, of the previous hour to now; meaning that if it was 11:35AM, it would return 10 AM to 11 AM:
DECLARE #today DATETIME, #hour DATETIME, #hourtwo DATETIME
SET #today = GETDATE()
-- Test other times
--SET #today = '2013-11-04 11:37.22'
SELECT #hour = DATEADD(hh,-2,#today)
SELECT #hourtwo = DATEADD(hh,-1,#today)
SELECT CONVERT(SMALLDATETIME,ROUND(CAST(#hour as float) * (24/1),0)/(24/1)) AS PreviousHourBegin
SELECT CONVERT(SMALLDATETIME,ROUND(CAST(#hourtwo as float) * (24/1),0)/(24/1)) AS PreviousHourEnd
This should work:
DECLARE #D DATETIME
SET #D = GETDATE()
SELECT #D AS 'Date',
DATEADD(HOUR,-1,DATEADD(MINUTE,-(DATEPART(MINUTE, #D)),DATEADD(SECOND,-(DATEPART(SECOND, #D)),DATEADD(MILLISECOND,-(DATEPART(MILLISECOND, #D)),#D)))) AS 'Range start',
DATEADD(MINUTE,-(DATEPART(MINUTE, #D)),DATEADD(SECOND,-(DATEPART(SECOND, #D)),DATEADD(MILLISECOND,-(DATEPART(MILLISECOND, #D)),#D))) AS 'Range end'
For the date:
2013-11-04 17:35:51.843
This will return a range like:
Start: 2013-11-04 16:00:00.000
End: 2013-11-04 17:00:00.000
For times between 00:00:00-01:00:00 it will get the range 23:00:00-00:00:00 from the previous day.
declare #today datetime
set #today=GETDATE()
select #today, DATEADD(HOUR, -2, DATEADD(HOUR, DATEDIFF(HOUR, 0, #today), 0)), DATEADD(MINUTE, -1, DATEADD(HOUR, -1, DATEADD(HOUR, DATEDIFF(HOUR, 0, #today), 0)))
result:
2013-11-04 17:42:17.933 2013-11-04 15:00:00.000 2013-11-04 15:59:00.000