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
 General SQL Server Forums
 New to SQL Server Programming
 Add minutes to a time

Author  Topic 

alanmac
Starting Member

26 Posts

Posted - 2011-07-22 : 07:43:37
Hi,

I have a time field in my database which stores data such as 14.30, 17.45, etc. What I need to do is get an integer from another field and add it to this time and return it in the same format, so that 14.15 plus 30 would return 14.45.

Is this possible?

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-07-22 : 08:23:38
[code]
Declare @t table (
tm decimal(5,2)
)

Insert Into @t Select 14.30
Insert Into @t Select 17.45
Insert Into @t Select 23.45


Declare @mn int

Set @mn = 30

Select
tm,
OneWay = convert(decimal(5,2),stuff(left(convert(varchar,dateadd(n,floor(floor(tm)*60 + tm*100%100)+@mn,35624),108),5),3,1,'.')),
Another = convert(decimal(5,2),((floor(tm)+floor((tm*100%100+@mn)/60))%24+24)%24 + (((tm*100%100+@mn)%60+60)%60)/100)
From @t
[/code]

Corey

I Has Returned!!
Go to Top of Page

alanmac
Starting Member

26 Posts

Posted - 2011-07-22 : 08:37:13
Many thanks.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-07-25 : 06:54:13
or
select convert(
varchar(10),
dateadd(minute,(floor(tm)*60+tm%1*100)+@mn,0),
108
)
from @t


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -