[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Formatting derived output in MS SQL view

Posted on 2014-04-28
2
Medium Priority
?
275 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 23

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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

650 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