Microsoft SQL 2005 -- cast / convert -- int to decimal ?

How can I change my below [PairedElapsedSeconds]
INT database column so it displays hours to one
decial places instead of rounding like the below
test1 and test2 do since it is an INT field ?

SELECT [PairedElapsedSeconds] / 60 as Total_Minutes
      ,CAST([PairedElapsedSeconds] AS varchar(5)) / 3660 as Total_Hrs_test1
      ,CONVERT(varchar(50),[PairedElapsedSeconds]) / 3660 as Total_Hrs_test2
  FROM [Transactions]
finance_teacherAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Two things going on here

1.

What you are experiencing is referred to as 'integer math', where SQL processes an integer divided by an integer and returns an integer, so 9 / 2 = 4 instead of 4.5.  To return the 4.5, one of the values needs to be support decimal values, i.e. 9 / 2.0 = 4.5So, copy paste the below into your SSMS, execute it to get the point across, then modify your code to use the CAST(.. as decimal)
Declare @x int = 9, @y int = 2

SELECT @x / @y

SELECT @x / CAST(@y as decimal(5,4)) 

Open in new window

2.

You'll want to convert the varchar's to numeric data types, and possibly test that they are numbers instead of a non-number that can't participate in a divide expression, using something like WHERE ISNUMERIC(column_name) =  or TRY_CAST
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PortletPaulfreelancerCommented:
    CREATE TABLE Transactions
        ([PairedElapsedSeconds] int)
    ;
        
    INSERT INTO Transactions
        ([PairedElapsedSeconds])
    VALUES
        (12345),
        (23456),
        (45678)
    ;
 

Open in new window

   
    select
          PairedElapsedSeconds
        , convert(varchar, PairedElapsedTime ,8) PairedElapsedTime
        , ca.PairedElapsedMinutes
        , ca.PairedElapsedHours
    from Transactions
    cross apply (
      select
          dateadd(second,PairedElapsedSeconds,0) PairedElapsedTime
        , PairedElapsedSeconds / 60.0 PairedElapsedMinutes
        , PairedElapsedSeconds / 3660.0 PairedElapsedHours
      ) ca
      
      

**[Results][2]**:
    | PairedElapsedSeconds | PairedElapsedTime | PairedElapsedMinutes | PairedElapsedHours |
    |----------------------|-------------------|----------------------|--------------------|
    |                12345 |          03:25:45 |               205.75 |            3.37295 |
    |                23456 |          06:30:56 |           390.933333 |           6.408743 |
    |                45678 |          12:41:18 |                761.3 |          12.480327 |

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.