?
Solved

Formatting derived output in MS SQL view

Posted on 2014-04-28
2
Medium Priority
?
268 Views
Last Modified: 2014-04-28
I have this derived column in a SQL view:

case when l.qty_comp != 0
then round(l.qty_scrap / l.qty_comp * 100, 0)
else 0
end [scrap_percent]

qty_comp and qty_scrap in the source table are both defined as decimal (13,4)

The output looks like this:

14.000000000000000000  (the actual results followed by the decimal point and 18 zeroes.

How can I get the output to look like a whole number, e.g, 14?

And, why does it format the data this way?



Thanks.
0
Comment
Question by:g_johnson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 2000 total points
ID: 40027319
If you always want it to appear as an integer:

case when l.qty_comp != 0
then cast(round(l.qty_scrap / l.qty_comp * 100, 0)) as integer
else 0
end [scrap_percent]

When doing integer division (14 / 7) the result will be an integer.
When doing decimal division (14.0 / 7.0) the result will be a decimal.

If you need one or the other, you need to cast the data type.
0
 
LVL 4

Author Comment

by:g_johnson
ID: 40027362
Thank you.  I thought I had tried that and gotten an error, but it works.  The error was coming from elsewhere ...
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question