Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2004
    Posts
    2

    Unanswered: Decimal places for integer divisions

    SELECT 1/2 returns 0 in transact sql instead of 0.500000

    How do I get it to return 0.5000 ?

  2. #2
    Join Date
    Mar 2004
    Posts
    8
    You need to force SQL Server to not do integer division. Try:

    SELECT CAST(1 AS float) / CAST(2 AS float)

  3. #3
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    You really have to understand datatypes. I still regularly get questions that resemble this one from developers who've been using SQL for years.

    In integer division, one divided by two equals zero with a remainder of one. When you divide one integer by another, integer math is used. If you cast either the dividend or the divisor to a real, then real math will be used (with implicit promotion if needed).

    -PatP

  4. #4
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Originally posted by calicocat
    You need to force SQL Server to not do integer division. Try:

    SELECT CAST(1 AS float) / CAST(2 AS float)
    Nah...it'll infer type if you let it...

    SELECT 1/2.0
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  5. #5
    Join Date
    Apr 2004
    Posts
    2
    A little more on my problem.

    I have two columns in a table each storing integer values. I would like the division to return a decimal number

    eg. select col1/col2 should return a decimal result... Neither of the above solutions work.

  6. #6
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    OK, so now you need to convert

    Code:
    USE Northwind
    GO
    
    CREATE TABLE myTable99(Col1 int, Col2 int)
    GO
    
    INSERT INTO myTable99(Col1,Col2)
    SELECT 1,2
    GO
    
    SELECT Col1/CONVERT(decimal(8,4),Col2)
      FROM myTable99
    GO
    
    DROP TABLE myTable99
    GO
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  7. #7
    Join Date
    Oct 2013
    Posts
    1
    select 1/2.0

  8. #8
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Quote Originally Posted by sudeeshig View Post
    select 1/2.0
    Congratulations on coming up with the exact same solution Brett Kaiser did nine and a half years ago.

    You should read through a thread (particularly old ones) before responding.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •