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)
 difference between to different types

Author  Topic 

inbs
Aged Yak Warrior

860 Posts

Posted - 2009-01-16 : 02:04:03
hi,

1.
i have 2 columns:

a - decimal
b- varchar

a           b
1034 22:56


how can i find the difference between them?(b-a-->22:56-10:34)

2.
if i want the difference in hours between two columns,what types need to be a/b?


thanks

thiyagu_rind
Starting Member

46 Posts

Posted - 2009-01-16 : 02:07:17
If am not wrong you want results as 12:22 Is that right?

Regards
Thiyagarajan
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-01-16 : 02:17:14
select replace(b,':','.')-a from urtable

2.
select datediff(hh,cast(replace(cast(10.36 as varchar(32)),'.',':') as datetime),cast('22:56' as datetime) )

select datediff(hh,cast(replace(cast(a as varchar(32)),'.',':') as datetime),cast(b as datetime) )
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2009-01-16 : 02:17:46
Use DATETIME Datatype for ur columns

Jai Krishna
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-16 : 02:35:54
[code]DECLARE @a DECIMAL(4, 0),
@b VARCHAR(5)

SELECT @a = 1034,
@b = '22:56'

SELECT CAST(@b AS DATETIME) - CAST(STUFF(REPLACE(STR(@a, 4), ' ', '0'), 3, 0, ':') AS DATETIME)[/code]


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

inbs
Aged Yak Warrior

860 Posts

Posted - 2009-01-16 : 05:39:47
is any type columns of TIME?

i get column b as 22:56 (from source) ,what should be column type destination?
is datetime get just field of time?
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-01-16 : 05:42:06
r u using 2005 r 2008
Go to Top of Page

inbs
Aged Yak Warrior

860 Posts

Posted - 2009-01-16 : 07:10:50
in this model i use 2000
Go to Top of Page

inbs
Aged Yak Warrior

860 Posts

Posted - 2009-01-16 : 07:46:35
if i get from source fields that means of them is hours.
what should be the types of the fields in destination?

as i say before

i get two fields

a - decimal
b- varchar


a           b
1034 22:56


thanks a lot who that help
Go to Top of Page
   

- Advertisement -