Solved

Formatting derived output in MS SQL view

Posted on 2014-04-28
2
260 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
2 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

726 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