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)
 casting needed from Varchar to numeric...?

Author  Topic 

clement.store
Starting Member

25 Posts

Posted - 2009-02-18 : 01:16:10
Hello there,

Why would casting be needed when i checked that casting from varchar to numeric is implicit in SQL server 2005?

And i had to use casting for subtraction as below.

SELECT UfCodeTo, UfCodeFrom, CAST(UfCodeTo AS numeric) - CAST(UfCodeFrom AS numeric) AS Expr1
FROM code_map
ORDER BY Expr1 DESC

Why can't I directly use UfCodeTo - UfCodeFrom?

Would anyone have some clues...?

Thanks
Clement

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-02-18 : 02:09:15
The real question is why you store codes as varchar, when you later want to do some arithmetic on them...



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

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-02-18 : 02:25:20
u can't use the subtract between two varchar fields
u will get error as Operand data type varchar is invalid for subtract operator.
so that u have to cast or convert ur fields in to numeric or decimal
AS peso said store the fields in numeric if there is any some arithmetic on them
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-02-18 : 04:48:05
<<
when i checked that casting from varchar to numeric is implicit in SQL server 2005?
>>

No

declare @v1 varchar(10),@v2 varchar(10)
select @v1='7652.43',@v2='7123.55'
select @v1-@v2

Madhivanan

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

clement.store
Starting Member

25 Posts

Posted - 2009-02-18 : 20:19:46
Thanks! I got it!
Go to Top of Page
   

- Advertisement -