|
|
Question : how can I convert number to string using variable thousand or decimal separators (comma vs period) ?
|
|
Hello all, I have an international DB, and I want to represent the numbers in it in different ways for different users. For example, the number 123,456.789 may be displayed as 123.456,789 or as 123 456, 789 in different locales. I tried to use UDF that parses the number in a loop and generates a string - takes number modulo 1000, adds thousand separator, etc. - but this is not fast enough, unfortunately... Is there a way to define converting rules for CONVERT/CAST function? Or maybe some other function that I can't find?
|
Answer : how can I convert number to string using variable thousand or decimal separators (comma vs period) ?
|
|
ok, let's split the job into 2 parts:
declare @v_variable numeric ( 20,10) set @v_variable = 123456789.123456789 select @v_variable , replace(convert(varchar,cast(cast(@v_variable as numeric(20,0)) as money),1),'.00','') . @v_variable - cast(@v_variable as numeric(20,0))
My output: 123456789.1234567890 123,456,789 .1234567890
Now, you can easily replace either the "," by "." or " " in the part before the decimal...
CHeers
|
|
|
|
|