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
 .NET Inside SQL Server (2005)
 Converting from varchar to double or integer

Author  Topic 

annas
Starting Member

36 Posts

Posted - 2008-04-03 : 12:08:33
Haiii..

I save number in database as varchar, but i want to call it back and
convert as double/int. Can it be done

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-04-03 : 12:11:44
Yes. use CAST or CONVERT for that.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

annas
Starting Member

36 Posts

Posted - 2008-04-04 : 01:12:43
Let say the i already convert it, but at the same time i also want to
sum all data in the row.

THank U
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-04 : 01:16:20
Provide a data example to illustrate your problem.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

annas
Starting Member

36 Posts

Posted - 2008-04-04 : 03:18:36
Example:
Let say this represent a price table in database. I declare the Price as Varchar
So i want to convert all the data into double and also sum it all

PRICE
_______
2.5
2.99
34.50
24.09
1.99

Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-04-04 : 05:18:21
How difficult is that?

Select sum(cast(price as decimal(12,2))) as Sum_Price
From table


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

annas
Starting Member

36 Posts

Posted - 2008-04-04 : 21:52:28
The code does work, but i don't want to save as decimal but as double data type.
I alter the code and put double. An error occur"Unable to parse query text."
Go to Top of Page

annas
Starting Member

36 Posts

Posted - 2008-04-04 : 21:56:33
Also is it possible to convert to decimal first and then to double
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-04 : 21:57:00
Double isn't a valid SQL Server data type. Check BOL for valid data types.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

annas
Starting Member

36 Posts

Posted - 2008-04-05 : 00:33:09
The problem is, event i sing a valid data type, like money or float. The result is the same
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-05 : 01:42:57
I don't understand what you mean.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-04-05 : 02:19:00
quote:
Originally posted by annas

The problem is, event i sing a valid data type, like money or float. The result is the same


Then you should post expected result for the sample data you have given

Madhivanan

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

annas
Starting Member

36 Posts

Posted - 2008-04-05 : 12:44:45
So what i want do is calculate some amount of price ok. Since i cannot declare as float, double or money in sql.
So i use varchar instead so the float no. can be save as float. To sum the number i have to convert it into decimal and sum.
But i want it to be convert back again into float. Can it be done
Go to Top of Page

annas
Starting Member

36 Posts

Posted - 2008-04-05 : 12:59:54
ok thanks everyone
some how i manage to doit. Since i building an application using vb.net and sql. When i sum it and retrieve back
to vb.net then i convert i back to double.
Go to Top of Page
   

- Advertisement -