10 feb 2017, Dominique Devienne:

There's
http://sqlite.1065341.n5.nabble.com/printf-with-thousands-separator-td85022.html

And my feeble attempt below. But there's got to be a better way, no?
What would be the shortest and/or most efficient way to do this in SQL?
..
sqlite> with s(v) as (
  ...>   select 23
  ...>   union all
  ...>   select 1097
  ...>   union all
  ...>   select 123456789
  ...>   union all
  ...>   select 4123456789
  ...> )
  ...> select v,
  ...> case
  ...> when v < 1000 then cast(v as text)
  ...> when v < 1000000 then printf("%d,%03d", v/1000, v%1000)
  ...> when v < 1000000000 then printf("%d,%03d,%03d", v/1000000,
v%1000000/1000, v%1000)
  ...> else printf("%d,%03d,%03d,%03d", v/1000000000,
v%1000000000/1000000, v%1000000/1000, v%1000)
  ...> end
  ...> from s
  ...> ;
23|23
1097|1,097
123456789|123,456,789
4123456789|4,123,456,789
sqlite>

Hello, I reply to the original mail as the question for a shorter/ moree efficient SQL solution isn't touched in the further discussion, Below is my attempt. Thanks, Edzard Pasma

SQLite version 3.16.2
select ltrim(substr(x,-9,3)||','||substr(x,-6,3)||','|| substr(x,-3,3),'0,') from (select 1234 as x union select 7 union select 123456789);
7
1,234
123,456,789

_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to