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)
 TYPE TIME

Author  Topic 

inbs
Aged Yak Warrior

860 Posts

Posted - 2009-01-17 : 11:01:18
if i have data in columns:

column1: 1042-decimal
coluimn2: 12:42-varchar

1.how i convert them to time i mean : 10:42:00
(what type of field that need to be keep ,i dont know if time exist?)

2.after i convert them ,i want to calculate the difference between them and get 2.

thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-17 : 11:21:50
1. use datetime field for both
2. use DATEDIFF(hh,col2,col2) to get 2
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-17 : 11:39:55
SELECT *, DATEDIFF(SECOND, newColumn1, newColumn2)
FROM (
SELECT Column1,
CAST(STUFF(REPLACE(STR(Col1, 4), ' ', '0'), 3, 0, ':') AS DATETIME) AS newColumn1,
Column2,
CAST(LEFT('0' + Column2, 4) AS DATETIME) AS newColumn2
FROM Table1
) AS d



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

inbs
Aged Yak Warrior

860 Posts

Posted - 2009-01-17 : 15:43:58
Peso
1
i do not understand your answer for column1,(what is order of the operation,what is doing first in
CAST(STUFF(REPLACE(STR(Col1, 4), ' ', '0'), 3, 0, ':') AS DATETIME) AS newColumn1) .

2
why do you add 0 for column2?

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-17 : 15:56:02
1) I take the numeric value and cast to varchar. Then I put a time delimeter betwen second and third position.
2) Just in case time is stored as varchar 9:42, with no leading zero.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -