2

I have values as string format below,

Values :

-24,52

-22,74

-13,08

303,75

When i convert above string values as money all values displays as below

-2452,00

-2274,00

-1308,00

30375,00

All values have more "0" number problem.How can i convert string values to exact money format ?

  • 3
    its not "money format" its money data type (basically: decimal). Formatting is a job for your UI, not your RDBMS – Jamiec Sep 22 '15 at 09:51
  • You should wash your data *before* casting, to avoid handling legitimate data afterwards. For the future (or even now if you can change it), if you need to store numbers in the database, don't pick a text data type. – Lasse V. Karlsen Sep 22 '15 at 09:55

4 Answers4

1

You can try to remove the 00 like this:

select replace(convert(varchar,cast(yourNum as money),1), ',00','')

Or in SQL Server 2012 you can try to use FORMAT

SELECT Format(24.01, '##,##0') 
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1
select cast('-24,01' as money) / 100

For anything other than visual presentation its a good idea to avoid MONEY entirely.

Community
  • 1
  • 1
Alex K.
  • 171,639
  • 30
  • 264
  • 288
1

try this query

WITH t
AS
(
    SELECT -24.52 AS [val]
    UNION
    SELECT -22.74
    UNION
    SELECT -13.08
    UNION
    SELECT 303.75
)

SELECT CAST( ABS(t.val) AS decimal(10,2)) -- or decimal(10,0)
FROM t
Chanom First
  • 1,136
  • 1
  • 11
  • 25
1

i tried to replace the comma with a point and got lucky:

cast(replace('-12,23', ',', '.') as money) 

the reason is, sql server treats ',' as thousend delimiter instead of decimal delimiter

A ツ
  • 1,267
  • 2
  • 9
  • 14