1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SQLiteだけで数値3桁カンマを付与する

Posted at

table名「table_name」のカラム「xxx」に3桁ずつカンマをつけて出力したい方法です。具体的には、tracでSQliteのみでなんとかしなきゃいけない場合に対応した方法です。日本円価格の数値への3桁対応のため、9ケタまで有効、小数点がある場合には非対応です。

使用前:123456789 → 使用後:123,456,789

select
 case
  when  length(xxx) > 6
     then substr(xxx,1,length(xxx)-6) || ',' || substr(xxx,length(xxx)-5,3) || ',' || substr(xxx,length(xxx)-2,3)
  when  length(xxx) > 3
     then substr(xxx,1,length(xxx)-3) || ',' || substr(xxx,length(xxx)-2,3)
  else  xxx
 end AS 'price'
from table_name

変更すべき変数:

  • xxx:元の数値
  • table_name:テーブル名
1
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?