Solved

SQL Server data type problem when dividing then printing...

Posted on 2008-06-23
2
224 Views
Last Modified: 2010-03-20
Hi,
I have the SQL statment below and the output is this:
24 / 115
0

I need the second number to be the result of the division - what am I doing wrong??

Thanks,
Tristan
--Calculate Conversion rate

		IF @CountUniqueVisits > 0 AND @CountPurchasers > 0

		BEGIN

			SET @ConversionRate = @CountPurchasers/@CountUniqueVisits

			PRINT CAST(@CountPurchasers AS VARCHAR) + ' / ' + CAST(@CountUniqueVisits AS VARCHAR)

			PRINT CONVERT(VARCHAR, @ConversionRate)

		END
 

OUTPUT:

24 / 115

0

Open in new window

0
Comment
Question by:Tristan Shortland
2 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 500 total points
ID: 21845129
Hello TShortland,

What have you declared @ConversionRate as? If it is INT then as the calculation results in a value of less than 1 it will be rounded to the nearest integer - (0).

You need to declare this as numeric instead:

Declare @ConversionRate Numeric(10,3)

For example.

Regards,

TimCottee
0
 
LVL 4

Author Comment

by:Tristan Shortland
ID: 21845158
That's done the trick.
Thank you!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

920 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

15 Experts available now in Live!

Get 1:1 Help Now