Hi there, How can i round off dates in TSQL (SQL Sever 2000). For example If the time is 12:13 then its considered as 12:10 and if time is 12:18 then time is considered as 12:20 Please advice. Thanks !
Try thisECLARE @d1 DATETIME,@div SMALLINT set @d1=getdate()set @div=(datepart(n,@d1))%10select CASE WHEN @div <=5 THEN DATEADD(n,-@div,@d1)WHEN @div >=5 THEN DATEADD(n,10-@div,@d1) END, @d1
[quote user="ranjitjain"] Try thisECLARE @d1 DATETIME,@div SMALLINT set @d1=getdate() set @div=(datepart(n,@d1))%10 select CASE WHEN @div <=5 THEN DATEADD(n,-@div,@d1) WHEN @div >=5 THEN DATEADD(n,10-@div,@d1) END, @d1 [/quote] You're not rounding the seconds and milliseconds. If that is a problem, probably somethng like this can work: SELECT DATEADD(minute, (DATEDIFF(minute, 0, @d1) + 5) / 10 * 10, 0), @d1