SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old July 2nd, 2009, 06:16 AM
idrisgani idrisgani is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Posts: 1 idrisgani User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 30 sec
Reputation Power: 0
Query - General - Round off a Real Value without Truncation

Hi,
I want to perform the following steps
1. Convert the real number into a character.
2. Convert the resulting character to a decimal.
3. Round the value at the N+1 th place.

My requirement is
When n=2
I want to round 18.005 to 18.01 or 18.0049 to 18.00(at 100th place)

when i try with

Declare @real real
Set @real=18.005
print Round(Convert(Decimal(15,7),Convert(varchar(30),@r eal)),2)

i can able to get the result until i get some indefinite real value like -4.8892562E-08.
so when i try the same logic, i am getting a conversion error(Error converting data type varchar to numeric.).

I noticed when i convert a real value of 19.05 to decimal(15,7) some of the digits are gets truncated. i am getting 19.0049991608 as result.
Please help me to get the problem resolved. thanks in advance

Reply With Quote
  #2  
Old July 9th, 2009, 02:19 PM
Lauramc's Avatar
Lauramc Lauramc is offline
SQL Slarentice
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2004
Location: In My Happy Place
Posts: 1,840 Lauramc User rank is General 4th Grade (Above 100000 Reputation Level)Lauramc User rank is General 4th Grade (Above 100000 Reputation Level)Lauramc User rank is General 4th Grade (Above 100000 Reputation Level)Lauramc User rank is General 4th Grade (Above 100000 Reputation Level)Lauramc User rank is General 4th Grade (Above 100000 Reputation Level)Lauramc User rank is General 4th Grade (Above 100000 Reputation Level)Lauramc User rank is General 4th Grade (Above 100000 Reputation Level)Lauramc User rank is General 4th Grade (Above 100000 Reputation Level)Lauramc User rank is General 4th Grade (Above 100000 Reputation Level)Lauramc User rank is General 4th Grade (Above 100000 Reputation Level)Lauramc User rank is General 4th Grade (Above 100000 Reputation Level)Lauramc User rank is General 4th Grade (Above 100000 Reputation Level)Lauramc User rank is General 4th Grade (Above 100000 Reputation Level)Lauramc User rank is General 4th Grade (Above 100000 Reputation Level)Lauramc User rank is General 4th Grade (Above 100000 Reputation Level)Lauramc User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 5 h 3 m 31 sec
Reputation Power: 1266
Are you taking this approach because the number of decimal places you need to round to is variable? If that is not the case you can simply use decimal(15,2) for two decimal places and that will solve your problem; although I have a hunch you want something more variable.

So I tried working with your example using float instead of real and here is what I did:
Code:
DECLARE @Num float
SET @Num = 18.005

DECLARE @DecPlaces int
SET @DecPlaces = 2

SELECT CAST(@Num as decimal(15,7))

DECLARE @SQLStr varchar(8000)
SET @SQLStr = 'SELECT CAST(' + CAST(@Num as varchar(50)) + ' as decimal(15,' + CAST(@DecPlaces as varchar(2)) + '))'
PRINT @SQLStr
EXEC (@SQLStr)
This results in 18.01 by the way, and using 19.05 results in... you guessed it 19.05. You'll want to do more testing, but I was also able to cast 19.05 to decimal(15,7) and I got 19.0500000 using float instead of real.

Give that a try and let me know if it works for you
__________________
Slarentice (origin:Shadow Wizard of ASP Free) [noun] A slave and apprentice of the Wizard's Circle (specifically of mehere) at ASP Free.
----
If shemzilla takes over, it's best to be on his good side


Reply With Quote
  #3  
Old October 20th, 2009, 05:28 AM
sakthi.tnj sakthi.tnj is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 23 sakthi.tnj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 48 m 35 sec
Reputation Power: 0
Re: Round Off a real value without truncation

Hi,

I hope that the below query will be useful for you


Declare
@real real,@n int, @sqlstr varchar(8000);
Set @real=18.005;
set @n = 1;
set @sqlstr = 'Select cast(' + cast(@real as varchar)+' as decimal (5,' + cast(@n as varchar)+'))'
exec (@sqlstr)


Thanks & Regards
Sakthimeenakshi.S

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > Query - General - Round off a Real Value without Truncation


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump





 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek