dbTalk Databases Forums  

convert from varchar to float

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss convert from varchar to float in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
shane
 
Posts: n/a

Default convert from varchar to float - 01-06-2004 , 12:16 AM






How do i conver from varchar to float

Reply With Quote
  #2  
Old   
Allan Mitchell
 
Posts: n/a

Default Re: convert from varchar to float - 01-06-2004 , 02:53 AM






What does your varchar value look like ?


declare @v varchar(10)
set @v = '123.4324'

select CAST(@v as float)

--

----------------------------
Allan Mitchell (Microsoft SQL Server MVP)
MCSE,MCDBA
www.SQLDTS.com
I support PASS - the definitive, global community
for SQL Server professionals - http://www.sqlpass.org



"shane" <anonymous (AT) discussions (DOT) microsoft.com> wrote

Quote:
How do i conver from varchar to float



Reply With Quote
  #3  
Old   
shane
 
Posts: n/a

Default Re: convert from varchar to float - 01-07-2004 , 01:11 AM



I have brought the data in from a csv file. Converted the colums to varchar 12.
Now i am trying to convert the columns so the that they are numeric. decimal so that i can calulate fields.
These fields will be used within a cube.
the data looks like the following.
Labour Hours varchar 12 and sample data looks like 0.10
rate varchar 12 and sample data looks like 24.30
amount varchar 12 and sample data looks like 2.51

I keep getting a error when trying to convert.
I have tried declaring a variable but this failed.


----- Allan Mitchell wrote: -----

What does your varchar value look like ?


declare @v varchar(10)
set @v = '123.4324'

select CAST(@v as float)

--

----------------------------
Allan Mitchell (Microsoft SQL Server MVP)
MCSE,MCDBA
www.SQLDTS.com
I support PASS - the definitive, global community
for SQL Server professionals - http://www.sqlpass.org



"shane" <anonymous (AT) discussions (DOT) microsoft.com> wrote

Quote:
How do i conver from varchar to float




Reply With Quote
  #4  
Old   
Allan Mitchell
 
Posts: n/a

Default Re: convert from varchar to float - 01-07-2004 , 02:18 AM



OK can you adapt this ?

CREATE TABLE IHaveNumerics (Col1 varchar(12))
INSERT IHaveNumerics VALUES('0.10')

select
col1,
CAST(col1 as decimal(5,2)) as DecimalVersion,
CAST(col1 as Numeric(5,2)) as NumericVersion
from
IHaveNumerics




--

----------------------------

Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP)
www.allisonmitchell.com - Expert SQL Server Consultancy.
www.SQLDTS.com - The site for all your DTS needs.
I support PASS - the definitive, global community
for SQL Server professionals - http://www.sqlpass.org


"shane" <anonymous (AT) discussions (DOT) microsoft.com> wrote

Quote:
I have brought the data in from a csv file. Converted the colums to
varchar 12.
Now i am trying to convert the columns so the that they are numeric.
decimal so that i can calulate fields.
These fields will be used within a cube.
the data looks like the following.
Labour Hours varchar 12 and sample data looks like 0.10
rate varchar 12 and sample data looks like 24.30
amount varchar 12 and sample data looks like 2.51

I keep getting a error when trying to convert.
I have tried declaring a variable but this failed.


----- Allan Mitchell wrote: -----

What does your varchar value look like ?


declare @v varchar(10)
set @v = '123.4324'

select CAST(@v as float)

--

----------------------------
Allan Mitchell (Microsoft SQL Server MVP)
MCSE,MCDBA
www.SQLDTS.com
I support PASS - the definitive, global community
for SQL Server professionals - http://www.sqlpass.org



"shane" <anonymous (AT) discussions (DOT) microsoft.com> wrote in message
news:4D6234BD-2A12-4D7C-88C1-04FE5A6060E0 (AT) microsoft (DOT) com...
How do i conver from varchar to float






Reply With Quote
  #5  
Old   
shane
 
Posts: n/a

Default Re: convert from varchar to float - 01-08-2004 , 04:21 AM



I created the table like you said and did a insert into.
I then tried to convert data but it failed.
What other steps can i use to clean this data and then convert to the required format.

Reply With Quote
  #6  
Old   
Allan Mitchell
 
Posts: n/a

Default Re: convert from varchar to float - 01-08-2004 , 04:33 AM



Can you show me your exact steps?

--

----------------------------

Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP)
www.allisonmitchell.com - Expert SQL Server Consultancy.
www.SQLDTS.com - The site for all your DTS needs.
I support PASS - the definitive, global community
for SQL Server professionals - http://www.sqlpass.org


"shane" <anonymous (AT) discussions (DOT) microsoft.com> wrote

Quote:
I created the table like you said and did a insert into.
I then tried to convert data but it failed.
What other steps can i use to clean this data and then convert to the
required format.




Reply With Quote
  #7  
Old   
shane
 
Posts: n/a

Default Re: convert from varchar to float - 01-08-2004 , 04:56 AM



I will start from the beginning , i import data into a table from a csv file.
From there i have convert the data to varchar 12 and imported into stg table 2 from here i am trying to convert the data into numeric or decimal using a view and the convert. But whenever i try to convert from varchar the convert fails.

Reply With Quote
  #8  
Old   
Allan Mitchell
 
Posts: n/a

Default Re: convert from varchar to float - 01-08-2004 , 05:35 AM



Show me a varchar(12) value that fails
show me the CONVERT statement
Show me the error/output
Show me the view.
Why is your table's column definition not a Numeric from the start. SQL
Server is prettty good at Implicitly converting values.

I.e.

1.234
CAST(col1 as Decimal(5,2)
"Invalid character for CAST ............"
CREATE VIEW.................

--

----------------------------

Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP)
www.allisonmitchell.com - Expert SQL Server Consultancy.
www.SQLDTS.com - The site for all your DTS needs.
I support PASS - the definitive, global community
for SQL Server professionals - http://www.sqlpass.org


"shane" <anonymous (AT) discussions (DOT) microsoft.com> wrote

Quote:
I will start from the beginning , i import data into a table from a csv
file.
From there i have convert the data to varchar 12 and imported into stg
table 2 from here i am trying to convert the data into numeric or decimal
using a view and the convert. But whenever i try to convert from varchar the
convert fails.




Reply With Quote
  #9  
Old   
shane
 
Posts: n/a

Default Re: convert from varchar to float - 01-08-2004 , 09:21 PM



Allen thanks for your help with this 1. I have located the problem. It starts at the source. Some of the fields have spaces in the cells. This is causing the error when trying to convert. I will have to construct the source data correctly remove all spaces from the cells and then sql will be able to convert to the format that i require...

cheers,

Reply With Quote
  #10  
Old   
Allan Mitchell
 
Posts: n/a

Default Re: convert from varchar to float - 01-09-2004 , 01:52 AM



Ahhh good so bad data is the issue.

What is the Source, a text file right.?

When you get it into SQL Server you could do

REPLACE(col, ' ','') and see if that gets rid of everything for you.

--

Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP)
www.allisonmitchell.com - Expert SQL Server Consultancy.
www.SQLDTS.com - The site for all your DTS needs.
I support PASS - the definitive, global community
for SQL Server professionals - http://www.sqlpass.org


"shane" <anonymous (AT) discussions (DOT) microsoft.com> wrote

Quote:
Allen thanks for your help with this 1. I have located the problem. It
starts at the source. Some of the fields have spaces in the cells. This is
causing the error when trying to convert. I will have to construct the
source data correctly remove all spaces from the cells and then sql will be
able to convert to the format that i require...
Quote:
cheers,



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.