PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 19, 2022

[FIXED] How to display numbers with decimal part equal to 0 as a whole number in T-SQL?

 July 19, 2022     decimal, integer, tsql     No comments   

Issue

Column1 Amount DesiredOutput
Item1 85.00 85
Item2 52.25 52.25
Item3 36.85 36.85
Item4 67.00 67

I want to be able to display numbers with decimal parts equal to 0 as whole numbers but the ones with decimal parts not equal to 0 as decimal numbers. I've tried this code but it didn't work:

 CASE 
      WHEN Amount % 1 = 0 THEN CAST (AMOUNT AS INT)
      ELSE Amount    
 END as [DesiredOutput]

The Amount column is DECIMAL type


Solution

Here is one solution

select *,
   case when
    amount = floor(amount) then FORMAT(amount, 'N0')
    else
    FORMAT(amount, 'N2')
    end as roundedPrice
from MyTable


Answered By - Thomas Koelle
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing