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
 SQL Server Development (2000)
 str vs varchar

Author  Topic 

lfmn
Posting Yak Master

141 Posts

Posted - 2001-11-29 : 11:35:39
I was writing some dynamic sql which required me to take a float, convert it so I could use it as part of a string and then perform calculations with it. I noticed that the float was being truncated at between 3 and 5 decimal places. I fixed it by using the str instead of convert function, but I'm curious if anyone knows why converting the float to varchar truncates the number? I've included a code sample to show what I'm talking about.

create table #value(value float(53))
insert into #value values(111.02536555454546)
select value as 'original' from #value
select convert(varchar(30), value) as 'varchar' from #value
select ltrim(str(value,28,25)) as 'str' from #value


cursors are like hammers - sometimes you have to use them, but watch your thumb!

robvolk
Most Valuable Yak

15732 Posts

Posted - 2001-11-29 : 11:51:24
Books Online has some entries under CONVERT() that describes its behavior. Real and float are handled differently from money and other numeric values. There are some format codes for formatting using CONVERT but I don't think you'll like the output (scientific notation). The default is a six digit maximum format, with sci-notation where applicable.

I don't know why they designed it this way. I always thought STR() was much better with number conversions than CONVERT() anyway.

Go to Top of Page

lfmn
Posting Yak Master

141 Posts

Posted - 2001-11-29 : 12:04:12
Thanks for the answer! I usually read BOL BEFORE I post a question, but this time I just skimmed (apparently to quickly).

quote:

Books Online has some entries under CONVERT() that describes its behavior. Real and float are handled differently from money and other numeric values. There are some format codes for formatting using CONVERT but I don't think you'll like the output (scientific notation). The default is a six digit maximum format, with sci-notation where applicable.

I don't know why they designed it this way. I always thought STR() was much better with number conversions than CONVERT() anyway.





cursors are like hammers - sometimes you have to use them, but watch your thumb!
Go to Top of Page
   

- Advertisement -