Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Error converting data type nvarchar to float


Error converting data type nvarchar to float

Author
Message
peter478
peter478
SSC Journeyman
SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)

Group: General Forum Members
Points: 83 Visits: 303
Hi I am testing one stored procedure.
I have this Test_table :

id int
name varchar
data float

I would like to update all records by condidtion:

Update Test_table set data = '27.55' where name = 'Peter'

When I tested it and appiers error with converting nvarchar to float .

Could you help me please.
Thanks
John-150025
John-150025
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 223
Hi, can you post the stored procedure code?
peter478
peter478
SSC Journeyman
SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)

Group: General Forum Members
Points: 83 Visits: 303
ALTER Procedure dbo.UPdata


(
@e_Nplan char(20),
@e_Ncas char(20)
)

As

DECLARE @Nplan as char(20)
DECLARE @Ncas as float(2)

SET @Nplan = @e_Nplan
SET @Ncas = CONVERT(float(2) , @e_Ncas)

Update Cis_OP_NO SET N_Cas100ks = @Ncas where N_Kplan =@Nplan

return
Christopher Stobbs
Christopher Stobbs
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1574 Visits: 2232
Ok you need to make some var changes.

You are using a CHar(20) which has a set lenght and SQL is struggling to convert that.

Have you tried changed @e_Ncas to a Float in the Procedure declaration?
I can't see any reason why it is a char to begin with...

Thanks
Chris

----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley

w00t
Posting Best Practices
Numbers / Tally Tables

SQL-4-Life
Sergiy
Sergiy
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5832 Visits: 11404
peter478 (5/19/2009)
ALTER Procedure dbo.UPdata


(
@e_Nplan char(20),
@e_Ncas char(20)
)

As

DECLARE @Nplan as char(20)
DECLARE @Ncas as float(2)

SET @Nplan = @e_Nplan
SET @Ncas = CONVERT(float(2) , @e_Ncas)

Update Cis_OP_NO SET N_Cas100ks = @Ncas where N_Kplan =@Nplan

return




To much typing.
Keep it short, do only what needs to be done.

ALTER Procedure dbo.UPdata
@e_Nplan char(20),
@e_Ncas char(20)
As

Update Cis_OP_NO
SET N_Cas100ks = @e_Nplan -- I don't see any point of passing a char(20) value from one var to another char(20) one
where N_Kplan = CONVERT(float(2) , LTRIM(RTRIM(@Nplan))) -- extra spaces in char variables may cause conversion to fail

GO


peter478
peter478
SSC Journeyman
SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)

Group: General Forum Members
Points: 83 Visits: 303
Hi Chris,

The reason, why I need to use type Char is that MS access doesnt work with Type float.For that reason I use Char.My entry for @e_Ncas is a number with comma. ( e.g. 15,79)
Of course when I change it to float it works but It doesnt solve my problem.
I only need to know how to convert char to float under SQL.Thats all.

Thank you
Peter
John-150025
John-150025
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 223
converting a char to a float will only work if the char string is in fact a valid number.

declare @string1 char(20), @string2 float

set @string1 = '15,76' -- THIS WILL FAIL
--set @string1 = '15.76' -- THIS WILL WORK

print @string1

set @string2 = convert(float,@string1)

print @string2
peter478
peter478
SSC Journeyman
SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)

Group: General Forum Members
Points: 83 Visits: 303
Hi Sergiy,

I have just tested your code but the same error message appiers.
When I use whole number e.g. 15 there is not problem, but if I use e.g. 15,28
appiers error message.

ALTER Procedure dbo.UpdateTB

@e_Nplan char(20), /* e.g. 112e */
@e_Ncas char(20) /* e.g. 15,28 */

As

Update Cis_OP_NO
SET N_Cas100ks = CONVERT(float(2) , LTRIM(RTRIM(@e_Ncas)))
Where N_Kplan = @e_Nplan

return

Thanks
peter478
peter478
SSC Journeyman
SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)

Group: General Forum Members
Points: 83 Visits: 303
Thank you John ,

now it works .... so I will have to modify my source code to substitute " , " to " . "
John-150025
John-150025
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 223
I was just gonna ask if your numbers were always formatted like '15,28' with commas then you could use

declare @string1 char(20), @string2 float

set @string1 = '152,23'

print @string1

set @string2 = convert(float,replace(@string1,',','.'))

print @string2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search