Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2010
    Posts
    51

    Unanswered: Format vs Convert

    I come from an Access background but am trying to learn SQLS. Most of it seems pretty straight forward aside from the varying functions. Which brings me to my question today.

    Format(value, "0.0"), Format(value, "0.000") both provide ways to display numbers according to the nearest place as defined in the second parameter.

    I have been reading over the Convert function for T-SQL, but I haven't noticed anything specific to place holders in the MSDN help search. money can have style 1-comma every third place away from the decimal, or 2 which gives digits accurate to four digits to the right of the decimal.

    Can anyone tell me how I would specify how many decimal places are presented in by using "convert"?

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Odds are good that you're using the wrong tool for the job. You most like want the Str() function. If you really do want to use Convert() or Cast(), then you probably want something like:
    Code:
    SELECT Cast(12.3456 AS NUMERIC(12,2))
    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  3. #3
    Join Date
    Oct 2010
    Posts
    51
    Well the Str looks like the easy way to do it, the downside being it returns a varchar instead of the numeric value that I need.

    So using the cast, how do you determine the decimal places? Is it as easy as
    Numeric(length, numberstoRightofDecimal)?
    Format "0.0000" = Numeric(x, 4)?
    Format "0.00" = Numeric(x,2)?

  4. #4
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Yep.

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  5. #5
    Join Date
    Oct 2010
    Posts
    51
    Thanks. I appreciate the help.

Tags for this Thread

Posting Permissions

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