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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 displaying time - solved

Author  Topic 

mrm23
Posting Yak Master

198 Posts

Posted - 2010-04-23 : 05:17:00
Hi All,

I have a DURATION column in one of my tables which is in nvarchar.
that column shows the duration alloted to a praticular task.

The data we get is in the form 10,20, 60 etc.

I want to store as 00:10 if it is 10 mins....
if it is 60 it must be 01:00.

How to do this?

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-04-23 : 05:22:14
[code]
select convert(varchar(5), dateadd(minute, duration, 0), 108)
from yourtable
[/code]

if the duration is more than 1 day, you might want to prefix that with days


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

mrm23
Posting Yak Master

198 Posts

Posted - 2010-04-23 : 05:24:24
Thanks KHTan. But the DURATION col is in NVARCHAR and i got the foll error:

Argument data type nvarchar is invalid for argument 2 of dateadd function.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-04-23 : 05:26:38
convert it to integer first .

select convert(varchar(5), dateadd(minute, convert(int, duration), 0), 108)
from yourtable



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

mrm23
Posting Yak Master

198 Posts

Posted - 2010-04-23 : 05:31:24
Thanks a lot! It works... i needed this urgently... thanks
Go to Top of Page

mrm23
Posting Yak Master

198 Posts

Posted - 2010-04-23 : 05:40:45
I am using this in a SP. I have a cursor in that and this conversion is to be made in that cusor query. now when i execute the SP, i am getting conversion error.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-04-23 : 05:42:11
post the lines of codes that gives the error


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

mrm23
Posting Yak Master

198 Posts

Posted - 2010-04-23 : 05:42:42
I think i need to give you one more detail. The client DB has that column in INT.
We must store as NVARCHAR.
So, the INT value 10 should be stored as NVARCHAR "00:10"
Go to Top of Page

mrm23
Posting Yak Master

198 Posts

Posted - 2010-04-23 : 05:44:21
My cursor query is :
SELECT tc.contactid,tc.companyname,
th.historytypeid,
th.endtime,
convert(nvarchar(5) ,dateadd(minute, th.duration, 0),108) ,
th.regarding,
tht.name
FROM tp.dbo.tbl_contact tc
JOIN tp.dbo.tbl_contact_history tch ON tc.contactid = tch.contactid
JOIN tp.dbo.tbl_history th ON th.historyid = tch.historyid
JOIN tp.dbo.tbl_historytype tht ON tht.historytypeid = th.historytypeid
WHERE category IS NOT NULL
AND th.historytypeid<>-1
***************************************************************************************************
This works fine individually but when i execute that i get the foll error:

Conversion failed when converting the nvarchar value '00:00' to data type int.
Go to Top of Page

mrm23
Posting Yak Master

198 Posts

Posted - 2010-04-23 : 05:50:13
Sorry it was my mistake. I have declared the variable wrongly.

Thanks for your time.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-04-23 : 06:02:24
you are welcome


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

mrm23
Posting Yak Master

198 Posts

Posted - 2010-05-12 : 07:28:40
hi all,
i have got the same issue ....
but now i need to only update that duration column.
it is in nvarchar datatype. so if i give convert(nvarchar(5),dateadd(minute,duration,0),108) ,
it is giving me the following error: Argument data type nvarchar is invalid for argument 2 of dateadd function.

I know this is very silly , but m blank... :( please help
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-05-12 : 07:54:31
try

convert(varchar(5), dateadd(minute, convert(int, duration), 0), 108)



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

mrm23
Posting Yak Master

198 Posts

Posted - 2010-05-12 : 07:56:43
hi khtan,
i tried this too...but i am getting the same error :(
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-05-12 : 08:43:14
quote:
Originally posted by mrm23

hi khtan,
i tried this too...but i am getting the same error :(


Post the full code you used

Madhivanan

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

mrm23
Posting Yak Master

198 Posts

Posted - 2010-05-12 : 08:48:53
hi all,
thanks for your time...

i got it...
i had to convert it to INTEGER first....

thanks
Go to Top of Page
   

- Advertisement -