Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Old Forums
 CLOSED - General SQL Server
 Datetime format manipulation

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-05-02 : 08:21:23
Lefteris writes "I have a column with a date format of the form
'yyyy-mm-dd HH:MM:SS.000' (e.g.'2006-04-05 17:20:30.623'). I would like this to be rounded up only to seconds and then sample every 10 seconds from the table.
(e.g. '2006-04-05 17:20:30,
'2006-04-05 17:20:40,
'2006-04-05 17:20.50 etc)

A colleague of mine suggested me to do the following

datepart(second,mydatecolumn)%10=0

but the problem is that when my datestring finishes at 29.57 I

miss it. I guess this approach will work fine if I can round up

the digits after the seconds?

I appreciate your help

Kind Regards

PS: I run on Windows 2000 Prof, MSSQL server 2000 (ver 7)"

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-05-02 : 09:05:39
[code]select dateadd(second, round(datepart(millisecond, datecol) / 1000.0,0), datecol)[/code]


KH

Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-05-02 : 11:20:01
This shows how to round time down to the nearest 10 seconds.

declare @day datetime
select @day = getdate()

select
ROUNDED_TO_10_SECOND =
dateadd(ms,-(((datepart(ss,@DAY)%10)*1000))-datepart(ms,@DAY),@DAY),
[TIME] = @day

Results:

ROUNDED_TO_10_SECOND TIME
----------------------- -----------------------
2006-05-02 11:19:10.000 2006-05-02 11:19:12.810

(1 row(s) affected)


CODO ERGO SUM
Go to Top of Page
   

- Advertisement -