Solved

Formatting derived output in MS SQL view

Posted on 2014-04-28
2
254 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now