dbTalk Databases Forums  

Formatting Numeric Value in Computed Column

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Formatting Numeric Value in Computed Column in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Stephen C. Smith
 
Posts: n/a

Default Formatting Numeric Value in Computed Column - 01-12-2008 , 05:09 PM






Hi there --

I'm writing a SQL query that works something like this:

SELECT a, b, a/b AS col_name FROM TABLE

Dividing a by b can result in a value above or below 1, e.g. 1.234 or 0.123.

If the value is below 1, I don't want the preceding zero, e.g. .123.

I've been using a STR() function to format it to three decimal points, e.g.:

STR(a/b, 5, 3) AS col_name

.... but for values below 1 it'll look like 0.123.

Any suggestions for how I can make this look like .123?

Thanks in advance.

Reply With Quote
  #2  
Old   
Tom van Stiphout
 
Posts: n/a

Default Re: Formatting Numeric Value in Computed Column - 01-12-2008 , 05:23 PM






On Sat, 12 Jan 2008 23:09:11 GMT, nospam (AT) nospam (DOT) com (Stephen C. Smith)
wrote:

Best to deal with that in the presentation layer, not in the data
layer.

-Tom.



Quote:
Hi there --

I'm writing a SQL query that works something like this:

SELECT a, b, a/b AS col_name FROM TABLE

Dividing a by b can result in a value above or below 1, e.g. 1.234 or 0.123.

If the value is below 1, I don't want the preceding zero, e.g. .123.

I've been using a STR() function to format it to three decimal points, e.g.:

STR(a/b, 5, 3) AS col_name

... but for values below 1 it'll look like 0.123.

Any suggestions for how I can make this look like .123?

Thanks in advance.

Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Formatting Numeric Value in Computed Column - 01-13-2008 , 04:39 AM



Stephen C. Smith (nospam (AT) nospam (DOT) com) writes:
Quote:
I'm writing a SQL query that works something like this:

SELECT a, b, a/b AS col_name FROM TABLE

Dividing a by b can result in a value above or below 1, e.g. 1.234 or
0.123.

If the value is below 1, I don't want the preceding zero, e.g. .123.

I've been using a STR() function to format it to three decimal points,
e.g.:

STR(a/b, 5, 3) AS col_name

... but for values below 1 it'll look like 0.123.

Any suggestions for how I can make this look like .123?
As Tom says, this is typically thing you should handle client-side. But
some times the only client is Query Analyzer or Mgmt Studio, in which case
that get kind of difficult.

You could do this:

SELECT a, b,
CASE WHEN b = 0 THEN NULL
WHEN abs(a) >= abs(b) THEN ltrim(str(a/b, 10, 3))
WHEN sign(a*b) = 1
THEN substring(ltrim(str(a/b, 10, 3)), 2, 10)
WHEN sign(a*b) = -1
THEN substring(ltrim(str(a/b, 10, 3)), 3, 10)
END
FROM tbl

Note: this is untested.

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #4  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: Formatting Numeric Value in Computed Column - 01-13-2008 , 01:16 PM



Another alternative is using something like this:

SELECT REPLACE(' ' + STR(a/NULLIF(b, 0), 5, 3), ' 0.', '.') AS col_name
FROM TABLE

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Reply With Quote
  #5  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: Formatting Numeric Value in Computed Column - 01-13-2008 , 01:40 PM



Forgot to note that if negatives are expected, then it needs one more nested
REPLACE:

SELECT REPLACE(REPLACE(' ' + STR(a/NULLIF(b, 0), 5, 3), ' 0.', '.'), '-0.',
'-.') AS col_name
FROM TABLE

Plamen Ratchev
http://www.SQLStudio.com


Reply With Quote
  #6  
Old   
Stephen C. Smith
 
Posts: n/a

Default Re: Formatting Numeric Value in Computed Column - 01-13-2008 , 06:00 PM



Thanks everyone who responded. You helped a lot. Those who said to resolve it
in the presentation layer were right. I was writing the front-end in ASP.NET
but couldn't find information on the DataFormatString variable to format it the
way I want. I finally found a site which showed how to do a custom format:

{0:#.000}

.... and that did the trick.

I wasn't able to find any information before on custom formats, which was why I
was trying to do it in SQL Server.

Again, thank you to everyone who stepped forward to help.

Stephen


On Sat, 12 Jan 2008 23:09:11 GMT, nospam (AT) nospam (DOT) com (Stephen C. Smith) wrote:

Quote:
Hi there --

I'm writing a SQL query that works something like this:

SELECT a, b, a/b AS col_name FROM TABLE

Dividing a by b can result in a value above or below 1, e.g. 1.234 or 0.123.

If the value is below 1, I don't want the preceding zero, e.g. .123.

I've been using a STR() function to format it to three decimal points, e.g.:

STR(a/b, 5, 3) AS col_name

... but for values below 1 it'll look like 0.123.

Any suggestions for how I can make this look like .123?

Thanks in advance.


Reply With Quote
Reply




Thread Tools
Display Modes

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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.