Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

how to you make this 44.8730084 into this: 44.8% Expand / Collapse
Author
Message
Posted Monday, December 28, 2009 11:33 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, August 31, 2010 4:54 PM
Points: 11, Visits: 22
use a select statement; how do you make this

44.8730084

into this

44.8 %

actually doesn't even have to be a percent ( % ) could simply
be a truncate of sorts to appear like this 44.8

any help, or direction on this would be great.

many thanks
Post #839530
Posted Monday, December 28, 2009 11:38 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 11:14 AM
Points: 2,041, Visits: 3,494
Go to BOL and look up numeric data types . . .
Post #839534
Posted Monday, December 28, 2009 11:41 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 11:14 AM
Points: 2,041, Visits: 3,494
99posts (12/28/2009)
use a select statement; how do you make this

44.8730084

into this

44.8 %

BTW, 44.873 would round to 44.9, not 44.8 (this is the math guy in me coming out here) . . .
Post #839535
Posted Monday, December 28, 2009 11:46 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, February 11, 2013 8:27 AM
Points: 1,646, Visits: 1,947
Ray is right. You would be getting 44.9 rather than 44.8

Start with this.

select ROUND(44.8730084,1) -- gives 44.9000000 as output


Or try this way.

declare @val float;

set @val = 44.8730084

select ROUND(@val,1,1) -- gives 44.8 as output
select ROUND(@val,1,0) -- gives 44.9 as output




Bru Medishetty

Blog -- LearnSQLWithBru

Join on Facebook Page Facebook.com\LearnSQLWithBru

Twitter -- BruMedishetty
Post #839540
Posted Monday, December 28, 2009 1:51 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, August 31, 2010 4:54 PM
Points: 11, Visits: 22
thanks for the replies... the value
i gave was just a sample not the actual results coming out of the query.


and it wouldn't be just 'that' value; would be applied to any value returned from
the 'percent' column.

i'm not that great with tsql, but from the looks of it the examples apply to just
one value and not the range queried ( forgive me if i misunderstand )

any how; what about truncating the 52.2027837 to something like this
52.2

any ideas?

thanks in advance
Post #839576
Posted Monday, December 28, 2009 2:03 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, October 26, 2010 9:36 AM
Points: 69, Visits: 23
select substring(cast(44.8730084 as varchar),1,4)+'%'
Post #839581
Posted Monday, December 28, 2009 2:05 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 11:14 AM
Points: 2,041, Visits: 3,494
Did you read BOL like I suggested? (Not trying to dance around the answer -- just trying to encourage you to look it up and figure it out on your own before asking questions, which is highly encouraged in this forum.)

There are a number of data types that will do this for you. For example, try DECIMAL.

Try messing with the numbers (including the 4 and 1 in the decimal declaration) in this example in your SQL and see what it does for you . . .

declare @number decimal(4,1)

select @number = 10.2436
select @number

Post #839583
Posted Monday, December 28, 2009 2:06 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 3:37 PM
Points: 40,210, Visits: 36,619
ravi_m_s (12/28/2009)
select substring(cast(44.8730084 as varchar),1,4)+'%'


Why would you want to convert to a varchar just to round a number off? String conversions are expensive operations.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #839584
Posted Monday, December 28, 2009 2:17 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, August 31, 2010 4:54 PM
Points: 11, Visits: 22
is ok... is a very small db.

let me see what i can do with these examples.

thanks to all!

:)

Post #839591
Posted Monday, December 28, 2009 2:20 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 11:14 AM
Points: 2,041, Visits: 3,494
If you're trying to convert from a table, try playing with this example:

declare @temp table(thisval float)

insert into @temp values (23.61)
insert into @temp values (1234.13456143)
insert into @temp values (100.00001)
insert into @temp values (12.34)
insert into @temp values (3.14159)
insert into @temp values (10.01)

select cast(thisval as decimal(6,2)) from @temp

Note: if I'm not mistaken (and someone correct me if I'm wrong), it's generally not advisable to put a function into a SELECT statement, since it can use processor time.
Post #839592
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse