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 2000 Forums
 Transact-SQL (2000)
 Type casiting problems

Author  Topic 

kam
Starting Member

17 Posts

Posted - 2006-03-06 : 18:25:42
Being a bit troubled with trying to convert a float value in to an nvarchar datatype. I'm attempting to use:
SET @text = convert(varchar, @xf)

but it seems to round the number up to the closest whole number.

Tried numerous combinations to see what result I get -

DECLARE @text nvarchar(50), @textTwo nvarchar(50), @xf float
SET @xf = 414193.446821874
SET @text = convert(varchar, @xf)
SET @textTwo = convert(varchar, 414193.446821874)


SELECT original = @xf
SELECT typecast = @text
SELECT typecastTwo = @textTwo



original
-----------------------------------------------------
414193.44682187401

(1 row(s) affected)

typecast
--------------------------------------------------
414193

(1 row(s) affected)

typecastTwo
--------------------------------------------------
414193.446821874

(1 row(s) affected)




Any ideas, suggestion?

cheers.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-03-06 : 18:58:04
You need to add a style to:

SET @text = convert(nvarchar(50), @original, styleGoesHere)

since the default is 0 with float. 0 is "Six digits maximum. Use in scientific notation, when appropriate.".

Style values for float are 0, 1, 2. Check out CONVERT function in SQL Server Books Online for details.

Tara Kizer
aka tduggan
Go to Top of Page

kam
Starting Member

17 Posts

Posted - 2006-03-07 : 19:08:35
Tkizer, Thanks for your reply.
Using styles results in exponential numbers, I'm trying to retrieve a decimal number in the form of a string.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-03-07 : 19:11:10
You won't be able to do that if you convert directly from float to varchar. Can't you have your application put the string in the correct format?

Tara Kizer
aka tduggan
Go to Top of Page
   

- Advertisement -