0

I have data as below

ColA
____ 
15.200000
16.210000
17.213000

Now I want output like below


15.20
16.21
17.213

how to achieve this

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
Sam
  • 21
  • 1
  • 2
    Presenatation should be handled within application layer. – Lukasz Szozda Jun 13 '18 at 12:54
  • Do you have more details on how you are determining what the number of decimals to go to? The first 2 are 2 places, but third is 3rd. Is it 2 places regardless of 0's, and if ending 0's then remove them if past 2 digitis? – Brad Jun 13 '18 at 12:55
  • Yes if you are not updating the data in the database then as Lukasz said you should not worry about formatting in SQL but that is done in presentation. – Brad Jun 13 '18 at 12:56
  • How to achieve by only using sql query ? (All the values are in different rows of a table) – Sam Jun 13 '18 at 12:56
  • @Brad I got this question in interview. – Sam Jun 13 '18 at 12:57
  • @Sam ok, there are ways to do it, but still need to know what your criteria is for number of decimal places to keep because your results you want are not the same (some are 2 and some are 3), or do you not have that info since it was in interview? If it didnt say what would you want us to try to do? – Brad Jun 13 '18 at 13:00
  • Personally, if I was asked that in a interview, I would ask them why would you want to? That likely means you'll be changing the data type from a `numeric` to a `varchar`; and that's an awful idea. Sometimes questions by interviewed are actually asked to see if you will question them, to test if you can identify "silly" requests. – Thom A Jun 13 '18 at 13:00
  • 1
    If 2012+ you can use format() the # are optional Example: select format(17.213000,'0.00#####') – John Cappelletti Jun 13 '18 at 13:05
  • The stated duplicate does not do what the OP is asking. – Gordon Linoff Jun 13 '18 at 13:10
  • @GordonLinoff there was an answer with `FORMAT` in it as well, which probably would have. But then, like I said in the comment above, that means that value is now a `varchar`, not a `numeric`; which is generally bad practice. – Thom A Jun 13 '18 at 13:19

0 Answers0