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
 Old Forums
 CLOSED - General SQL Server
 convert

Author  Topic 

DENIZ3E
Yak Posting Veteran

56 Posts

Posted - 2005-04-18 : 13:50:40
hý everybody,
I WANT TO VARCHAR CONVERT TO FLOAT BUT...

SELECT CONVERT(FLOAT,REPLACE('P1*100','P1','1000'))

'Server: Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to float.

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2005-04-18 : 14:04:42
'1000*100' is not a string that can be converted diretly to a number:
SELECT CONVERT(FLOAT,'1000*100')

Server: Msg 8114, Level 16, State 5, Line 4
Error converting data type varchar to float.


You should do it this way:
select CONVERT(FLOAT,'1000')*CONVERT(FLOAT,'100')


CODO ERGO SUM
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-04-18 : 14:06:40
You also don't need to post to multiple forums
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=48553

Be One with the Optimizer
TG
Go to Top of Page

DENIZ3E
Yak Posting Veteran

56 Posts

Posted - 2005-04-18 : 17:44:01
BUT I DONT KNOW '1000*100'
THIS CAN BE '1000*3*2' OR '10/10/1' OR....
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2005-04-18 : 18:04:40
CONVERT is not going to work with strings that have to be computed. It will only work with strings that can be converted directly to a number.

quote:
Originally posted by DENIZ3E

BUT I DONT KNOW '1000*100'
THIS CAN BE '1000*3*2' OR '10/10/1' OR....



CODO ERGO SUM
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-04-18 : 19:21:53
don't know if this will help, but it might get you thinking in a different direction:

declare @str varchar(20)
,@val float
,@sql nvarchar(50)

set @str = 'P1*100'
set @sql = 'Select @val = ' + replace(@str,'P1','1000')

exec sp_executesql @sql, N'@val float output', @val = @val output
select @val


Be One with the Optimizer
TG
Go to Top of Page

DENIZ3E
Yak Posting Veteran

56 Posts

Posted - 2005-04-19 : 01:59:10
VERY VERY THANKS
Go to Top of Page
   

- Advertisement -