473,387 Members | 3,684 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Formatting Numeric Value in Computed Column

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.
Jan 12 '08 #1
5 3549
On Sat, 12 Jan 2008 23:09:11 GMT, no****@nospam.com (Stephen C. Smith)
wrote:

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

-Tom.
>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.
Jan 12 '08 #2
Stephen C. Smith (no****@nospam.com) writes:
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, es****@sommarskog.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
Jan 13 '08 #3
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
Jan 13 '08 #4
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

Jan 13 '08 #5
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, no****@nospam.com (Stephen C. Smith) wrote:
>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.
Jan 14 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: DMAC | last post by:
If i want to split a computed column into two or more columns based on the the length (its a varchar) of the computed column, how often will sql server determine what the computed column is?...
4
by: Dave Brydon | last post by:
Access 2003 I have a combo box in my personnel table, which draws its data from a trade code table; the original field in the code table, is numeric, Long Integer, and formatted with 5 zero's . ...
7
by: BBFrost | last post by:
I'm receiving decimal values from database queries and placing them on a report page. The users want to see the following .... Db Value Display Value 123.3400 123.34...
9
by: Coleen | last post by:
Hi All :-) I found the way to get my column sum (Thanks Cor I did it a little different, but the result is what I wanted) I used: dt_stat_report_3b.Columns.Add(New DataColumn("Sum",...
2
by: Starbuck | last post by:
Hi All Using VB.Net with a datagrid which shows a column called status - the values can be 0 to 6. Is there anyway of formatting this column show the numeric value is translated to a more...
5
by: Edwin Knoppert | last post by:
I want to format a hyperlink bounded field. Fields *could* have http:// *or not* in the websitename entered. I want to format this to http://website.com otherwise i'll get a relative link. I...
2
by: David Veeneman | last post by:
How does one format a date column in a GridView control? I had assumed that the DataFormat string would do it, but MSDN only shows numeric formatting codes. Can dates be formatted using that...
7
by: Aamir Mahmood | last post by:
Hi All I have DataTable object. Is there a way that I can know which fields (columns) in the table are computed. Apparantly the DataTable.Columns returns all columns both computed and other....
1
by: mik357 | last post by:
Hello all, I just started using SQL to create a report. I got the syntax working but I have absolutely no idea of how to format data. Can anyone kindly look at my code and help me? I thank you...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.